0
編輯:我的錯誤問題是我沒有url編碼字符串,即xdoc.Load(Server.UrlEncode(「website.com」)) ;從沒有doc類型的URL解析XML:什麼是錯誤
在C#ASP.NET中,我從URL中檢索XML。該XML的格式爲如下所示:
<html>
<head></head>
<body>
<product>
<course>
</course>
</product>
... more data
</body>
</html>
所以它真的HTML或者最好不是有效的XML。當我加載這個來自Internet上的服務器的xml時,我收到錯誤消息。
解析/加載失敗是因爲XML無效(HTML),還是失敗,因爲我從URL而不是本地文件檢索XML?
我該如何成功解析這個文件?我是否需要添加XML文檔類型,我是否使用C#HTML解析器?
的錯誤是:
// ERROR: Data at the root level is invalid. Line 1, position 1.
public string getProductXML()
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load("http://www.website.com/test.aspx?a=1&b=2"); //ERROR HERE: Data at the root level is invalid. Line 1, position 1.
// NOTE www.website.com is a different server to where this code is executing
XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("group");
}
你確定你正在得到正確的XML嗎?嘗試手動下載使用'WebClient'然後'LoadXml()'得到的字符串。 – svick 2012-02-24 01:07:03