在回答这个问题时,我遇到了一个我不明白的情况。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 声明省略了编码。