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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0