ASP.net downloads XML file from URL

Trying to 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.

Should I first execute an XMLHTTP request to a file? Or am I right to suggest that I can download it from an external URL?

+4
source share
2 answers

try the following:

 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 expects an xml string directly, where Load can use uri to capture xml data. With your code, the xml parser actually tried to parse the address as xml, not the content in the uri location.

[Edit] , you can take a look at the built-in .Net Framework feed handling classes. These classes are located in the System.ServiceModel.Syndication namespace . They can easily work with parsing work.

+8
source

I found this link in yahoo very useful and simple. Neat !!!

http://developer.yahoo.com/dotnet/howto-xml_cs.html

+9
source

Source: https://habr.com/ru/post/1341113/


All Articles