如何从URL加载XML上的XmlDocument() [英] How to load XML from URL on XmlDocument()

查看:295
本文介绍了如何从URL加载XML上的XmlDocument()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的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 :

异常详细信息:System.Xml.XmlException:在根级别的数据无效。 1号线,位置1。

为什么呢?我在哪里错了?我怎么能固定在C#这个问题?

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

还与尝试:

myXmlDocument.Load(m_strFilePath);    

但我得到:

异常详细信息:System.Xml.XmlException:无效字符在给定的编码。 1号线位置503。

推荐答案

它告诉你, m_strFilePath 的值不是有​​效的XML。尝试:

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

然而,这是失败的(不明原因......似乎是在 A Umidità呛)。以下作品(仍在试图找出有什么区别虽然):

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

这篇关于如何从URL加载XML上的XmlDocument()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆