4

Trying to just simply parse an XML file;

    protected void Page_Load(object sender, EventArgs e)
    {

        XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing

        xdoc.LoadXml("http://latestpackagingnews.blogspot.com/feeds/posts/default");//loading XML in xml doc

        XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("entry");//reading node so that we can traverse thorugh the XML

        foreach (XmlNode xNode in xNodelst)//traversing XML 
        {
            litFeed.Text += "read";
        }

    }

But I get:

Data at the root level is invalid. Line 1, position 1.

Do I have to do an XMLHTTP request to the file first? Or am I right to assume I can load it in from an external url?

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456

1 Answers1

8

try this :

protected void Page_Load(object sender, EventArgs e)
{

    XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing

    xdoc.Load(
        "http://latestpackagingnews.blogspot.com/feeds/posts/default"
        );//loading XML in xml doc

    XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("entry");//reading node so that we can traverse thorugh the XML

    foreach (XmlNode xNode in xNodelst)//traversing XML 
    {
        litFeed.Text += "read";
    }

}

LoadXml is waiting for an xml string directly, where Load can use an uri to grab the xml data. With your code, the xml parser was actually trying to parse the adress as xml, not the content at the uri location.

[Edit] you may take a look at the builtin feed processing classes of the .Net Framework. These classes are in the System.ServiceModel.Syndication namespace. They can to the parsing job for you quite easily.

Steve B
  • 36,818
  • 21
  • 101
  • 174
  • 1
    Almost correct ... just remove the "new Uri()" bit to my eyes. Load accepts a URL directly, but not a URI object. – Jeff Parker Feb 24 '11 at 09:50
  • Neither of those work... and @Jeff, removing the new URI line just is exactly the same as the code in my original question!?!?!? – Tom Gullen Feb 24 '11 at 09:54
  • @Tom Gullen: I replaced LoadXml method by Load method. The former waits for xml data, the later waits for a location where to find xml data – Steve B Feb 24 '11 at 09:57
  • Ahhh got you. I'm expecting it to say 'readreadread' over and over but the page is blank... no errors though. – Tom Gullen Feb 24 '11 at 09:59
  • @Tom Gullen: now I think you xpath query is not valid. Either replace it by the correct one, or take a look at the .net builtin feed reading provided by classes in [System.ServiceModel.Syndication](http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.aspx) – Steve B Feb 24 '11 at 10:03
  • I'm baffled now :-( All I want to do is read that Atom feed from blogger.com and display it on my webpage. – Tom Gullen Feb 24 '11 at 10:05
  • if you simply want to display rss content, take a look at [this article](http://www.intrepidstudios.com/blog/2008/8/21/rss-using-dot-net-and-xmldatasource-namespaces.aspx). Just change the xpath query to match the atom format instead of the rss format – Steve B Feb 24 '11 at 10:08
  • @Steve, thanks, I don't understand what I need to put in the format, is it 'feed/entry' because that doesn't work – Tom Gullen Feb 24 '11 at 10:39