darrrrUC
darrrrUC

Reputation: 311

XmlTextReader only works in console application?

I have a rss-reader in a console application, and it works just fine as such:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlTextReader reader = new  XmlTextReader("http://www.di.se/rss");
            string myXmlString = "";
            // Skip non-significant whitespace  
            reader.WhitespaceHandling = WhitespaceHandling.Significant;

            // Read nodes one at a time  
            while (reader.Read())
            {
                // Print out info on node  
                //Console.WriteLine(reader.ReadInnerXml().ToString());
                myXmlString += myXmlString + reader.ReadInnerXml().ToString();

            }

            //Create the XmlDocument.
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<root>" + myXmlString + "</root>");

            //Display all the book titles.
            XmlNodeList elemList = doc.GetElementsByTagName("title");
            for (int i = 0; i < elemList.Count; i++)
            {
                Console.WriteLine(elemList[i].InnerXml);
            }

            Console.ReadLine();
        }


    }
}

However when I try to move it over to my Windows Universal application it says that XmlTextReader namespace does not exist. Does this only work in console apps? How could I try to translate this to my universal application? I do have the exact same namespaces declared in my universal app as in my console project (and then some.) So what I am trying to do is get this to run in my mainpage.xaml.cs file.

I tried using XmlReader reader = new XmlReader("http://www.di.se/rss"); instead but to no avail.

Upvotes: 0

Views: 722

Answers (5)

Phil Blackburn
Phil Blackburn

Reputation: 1067

I think the problem lies with XmlUrlResolver which XmlReader uses under the covers. The default UrlResolver does not have any user credentials so you can only reach out to locations that do not require credentials. I expect that in switching from console to Universal the security requirements for external resoursces have become more robust. You may need to reference the XmlSecureResolver (or similar) in your XmlReaderSettings. Check out the remarks section here: https://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings(v=vs.110).aspx

Upvotes: 1

Pavel Bory
Pavel Bory

Reputation: 36

I have probably reproduced the issue. In let’s say common Console Application, that you create using the default Console Application Template from Visual Studia, the System.Xml assembly is referenced. But in UWP it’s not referenced in default template and the assembly System.Xml cannot be simply referenced from UWP like you did in the Console Application.

To solve the issue, you should follow these steps: Add NuGet Package for work with XML like System.Xml.ReaderWriter to your UWP Application.

Work with XmlReader is a little bit different, because you can’t simple access the URI like you did in Console Application. You should Create HttpClient, obtain a Stream and pass it to the XmlReader. Here I put a sample code-behind for Main.xaml.cs which comes with default UWP Application Template:

using System.Net.Http;
using System.Xml;
using Windows.UI.Xaml.Controls;

namespace App1
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            var client = new HttpClient();
            var stream = client.GetStreamAsync("http://www.di.se/rss").Result;

            XmlReader reader = XmlReader.Create(stream);
            string myXmlString = "";
            while (reader.Read())
            {
                myXmlString += myXmlString + reader.ReadInnerXml().ToString();
            }

            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<root>" + myXmlString + "</root>");

            //Display all the book titles.
            XmlNodeList elemList = doc.GetElementsByTagName("title");
            for (int i = 0; i < elemList.Count; i++)
            {
                var xml = elemList[i].InnerXml;
                // Do your work with xml
            }
        }
    }
}

Upvotes: 2

JDavila
JDavila

Reputation: 449

Make sure your building your project with needed libraries for XMLReader, might have to add the library in the new app, though this should not be needed for a universal app.

Upvotes: 0

JeffFerguson
JeffFerguson

Reputation: 3002

Call Create() on an XmlReader to get a usable stream, as in the following example:

        using (var reader = XmlReader.Create(path))
        {
            this.XmlDocument.Load(reader);
        }

Upvotes: 2

Tim Friesen
Tim Friesen

Reputation: 355

Universal applications should support the XmlReader just fine. Confirm that you have System.Xml assembly referenced in the project.

Upvotes: 0

Related Questions