我正在编写一个简单的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>";
}
}
offer是一个div元素。 当我运行此代码时,我得到一个“System.Xml.XmlException:给定编码中的无效字符。行8271,位置163.”错误 如您所见,我还使用ourBlog.Declaration.Encoding =“”设置了编码。 我试过了:
我不知道还有什么可以尝试的。 你有什么建议吗?
修改
Stack Trace是:
来源错误:
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
答案 0 :(得分:1)
好的,我在这里找到了一个有效的解决方案。
这是新代码:
protected void LoadXML()
{
var wc = new WebClient();
using (var sourceStream = wc.OpenRead("http://www.cpalead.com/dashboard/reports/campaign_rss.php?id=187000"))
{
using (var reader = new StreamReader(sourceStream))
{
XDocument ourBlog = XDocument.Load(reader);
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 = XmlConvert.VerifyXmlChars(item.Element("description").Value),
amount = item.Element(NameSpace + "amount").Value,
campid = item.Element(NameSpace + "campid").Value,
country = item.Element(NameSpace + "country").Value,
type = item.Element(NameSpace + "type").Value,
epc = item.Element(NameSpace + "epc").Value,
ratio = item.Element(NameSpace + "ratio").Value
};
foreach (var item in XMLItem)
{
offers.InnerHtml += item.title + " : " + item.description + " : " + item.amount + "<br />";
}
}
}
}
希望这将有助于其他人。
答案 1 :(得分:0)
您的XML文件确实无效。它的编码显然是UTF-8。但是第8271行存在问题。
这条线基本上是这样的:
<description>eMusic δίνει οπαδούς μουσικής της φανταστική συναλλάσσεται για μεγάλη μουσική, κατά μέσο όρο περίπου τα μισά από Amazon ή το iTunes κατάστημα. Έναρξ_</description>
但是在我放下划线的结束标记之前,数据似乎在UTF-8多字节字符的中间被截断。在十六进制中它看起来像这样:
CF 81 CE BE CE 3C 2F 64 65
CF 81 CE BE是希腊字母ρξ
,3C 2F 64 65是</de
。但剩余的CE启动一个被切断的多字节序列。该值被切断为255个字节!
您需要修复源文件。它无效。在IT中,255字节不是随机长度。可能缺少更多数据。