C# - How to load XML from URL on XmlDocument()

I have this code:

string m_strFilePath = "http://www.google.com/ig/api?weather=12414&hl=it";

XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.LoadXml(m_strFilePath);

foreach (XmlNode RootNode in myXmlDocument.ChildNodes)
{
}

but when I try to execute it, I get this error :

Exception Details: System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.

Why? Where am I wrong? And how can I fix this problem on C#?

Also tried with :

myXmlDocument.Load(m_strFilePath);    

but I get :

Exception Details: System.Xml.XmlException: Invalid character in the given encoding. Line 1, position 503.

Solution

It's telling you that the value of m_strFilePath is not valid XML. Try:

string m_strFilePath = "http://www.google.com/ig/api?weather=12414&hl=it";
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(m_strFilePath); //Load NOT LoadXml

However, this is failing (for unknown reason... seems to be choking on the à of Umidità). The following works (still trying to figure out what the difference is though):

var m_strFilePath = "http://www.google.com/ig/api?weather=12414&hl=it";
string xmlStr;
using(var wc = new WebClient())
{
    xmlStr = wc.DownloadString(m_strFilePath);
}
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);

Mirror from

StackOverflow - How to load XML from URL on XmlDocument() - https://stackoverflow.com/questions/7496913/how-to-load-xml-from-url-on-xmldocument/7496968#7496968

Previous Post Next Post