【发布时间】:2011-09-21 18:05:01
【问题描述】:
在回答this question 时,我遇到了一个我不明白的情况。 OP 试图从以下位置加载 XML:http://www.google.com/ig/api?weather=12414&hl=it
显而易见的解决方案是:
string m_strFilePath = "http://www.google.com/ig/api?weather=12414&hl=it";
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(m_strFilePath); //Load NOT LoadXml
但是这失败了
XmlException : 给定编码中的无效字符。第 1 行,位置 499。
Umidità 的 à 似乎卡住了。
OTOH,以下工作正常:
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);
我对此感到困惑。谁能解释为什么前者失败,但后者工作正常?
值得注意的是,文档的 xml 声明省略了编码。
【问题讨论】:
-
WebClient htmlencodes 有可能吗?
标签: c# load xmldocument xmlexception