How to load XML from URL on XmlDocument()

You need to use Load() instead of LoadXML(). LoadXML tries to parse a string into XML, in this case your URL.


NOTE: You're really better off using XDocument for most XML parsing needs nowadays.

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);

Tags:

C#

Xml