我写一个简单的XML解析器这将通过这个XML输出: http://www.cpalead.com/dashboard/reports/campaign_rss.php?id=187000
完整的C#代码为:
protected void LoadXML()
{
XDocument ourBlog = XDocument.Load("http://www.cpalead.com/dashboard/reports/campaign_rss.php?id=187000");
ourBlog.Declaration.Encoding = "ISO-8859-1";
XNamespace NameSpace = "http://www.cpalead.com/feeds/campinfo.php";
var XMLItem = from item in ourBlog.Descendants("item")
select new
{
title = item.Element("title").Value,
link = item.Element("link").Value,
guid = item.Element("guid").Value,
description = item.Element("description").Value,
campinfoamount = item.Element(NameSpace + "amount").Value,
campinfocampid = item.Element(NameSpace + "campid").Value,
campinfocountry = item.Element(NameSpace + "country").Value,
campnfotype = item.Element(NameSpace + "type").Value,
campinfoepc = item.Element(NameSpace + "epc").Value,
campinforatio = item.Element(NameSpace + "ratio").Value
};
foreach (var item in XMLItem)
{
offers.InnerHtml += item.title + item.campinforatio + "<br>";
}
}
提供是一个div元素。 当我运行此代码,我得到一个“System.Xml.XmlException:无效字符在给定的编码8271线,位置163” 错误正如你可以看到我也用ourBlog.Declaration.Encoding =“”集编码; 我努力了:
- ISO-8859-1
- UTF-8
- 窗户-1251
- 窗口1252
- UTF-16
我不知道什么尝试。 你有什么建议吗?
编辑:
堆栈跟踪:
源错误:
Line 19: protected void LoadXML()
Line 20: {
Line 21: XDocument ourBlog = XDocument.Load("http://www.cpalead.com/dashboard/reports/campaign_rss.php?id=187000");
Line 22: ourBlog.Declaration.Encoding = "ISO-8859-1";
Line 23: XNamespace NameSpace = "http://www.cpalead.com/feeds/campinfo.php";
堆栈跟踪:
[XmlException: Invalid character in the given encoding. Line 8271, position 163.]
System.Xml.XmlTextReaderImpl.Throw(Exception e) +69
System.Xml.XmlTextReaderImpl.Throw(String res, String arg) +116
System.Xml.XmlTextReaderImpl.InvalidCharRecovery(Int32& bytesCount, Int32& charsCount) +197
System.Xml.XmlTextReaderImpl.GetChars(Int32 maxCharsCount) +131
System.Xml.XmlTextReaderImpl.ReadData() +188
System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars) +482
System.Xml.XmlTextReaderImpl.FinishPartialValue() +62
System.Xml.XmlTextReaderImpl.get_Value() +74
System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r) +505
System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o) +48
System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options) +283
System.Xml.Linq.XDocument.Load(String uri, LoadOptions options) +58
System.Xml.Linq.XDocument.Load(String uri) +6
WebApplication3.Earn._default.LoadXML() in c:\Users\WinDrop\Documents\Visual Studio 2013\Projects\WebApplication3\WebApplication3\Earn\default.aspx.cs:21
WebApplication3.Earn._default.Page_Load(Object sender, EventArgs e) in c:\Users\WinDrop\Documents\Visual Studio 2013\Projects\WebApplication3\WebApplication3\Earn\default.aspx.cs:16
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772