问题描述
我正在编写一个简单的XML解析器来传递这个XML输出:完整的C#代码是:
protected void LoadXML()
{
XDocument ourBlog = XDocument.Load(http://www.cpalead。 ?COM /仪表板/报告/ campaign_rss.php ID = 187000\" );
ourBlog.Declaration.Encoding =ISO-8859-1;
XNamespace NameSpace =http://www.cpalead.com/feeds/campinfo.php;
var XMLItem = fromBlog.Descendants(item)中的项目
选择新
{
title = item.Element(title)。值,
link = item.Element(link)。value,
guid = item.Element(guid)。value,
description = item.Element(description)。Value,$ b $值$,
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(XMLItem中的var项目)
{
offers.InnerHtml + = item.title + item.campinforatio +< br>;
}
}
offer是一个div元素。
当我运行这个代码时,我得到一个System.Xml.XmlException:给定编码中无效的字符,行8271,位置163。错误
正如你所看到的,我也使用我们的Blog.Declaration.Encoding =;
我尝试过:
- ISO-8859-1
- UTF-8
- windows-1251
- windows 1252
- UTF-16
我不知道还有什么要尝试的。
您有任何建议吗?
编辑:
跟踪是:
源错误:
第19行:protected void LoadXML()
行20:{
行21:XDocument ourBlog = XDocument.Load(http://www.cpalead.com/dashboard/reports/campaign_rss.php?id=187000);
第22行:ourBlog.Declaration.Encoding =ISO-8859-1;
第23行:XNamespace NameSpace =http://www.cpalead.com/feeds/campinfo.php;
堆栈跟踪:
[XmlException:给定编码中的无效字符。 Line 8271,position 163。]
System.Xml.XmlTextReaderImpl.Throw(Exception e)+69
System.Xml.XmlTextReaderImpl.Throw(String res,String arg)+116
系统。 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
系统.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()在c:\\ \\Users\WinDrop\Documents\Visual Studio 2013\Projects\WebApplication3\WebApplication3\Earn\default.aspx.cs:21
WebApplication3.Earn._default.Page_Load(Object sender,EventArgs e)在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
好的,我在这里找到了一个工作解决方案。
这是新的代码:
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 = fromBlog.Descendants(item)中的项目
选择新
{
title = item.Element(title)。值,
link = item.Element(link)。value,
guid = item.Element(guid)。value,
description = XmlConvert.VerifyXmlChars(item.Element(description )
amount = item.Element(NameSpace +amount)Value,
campid = item.Element(NameSpace +campid)。value,
country = item.Element Value,
type = item.Element(NameSpace +type)。value,
epc = item.Element(NameSpace +epc)。值,
ratio = item.Element( NameSpace +ratio)。value
};
foreach(XMLItem中的var项目)
{
offers.InnerHtml + = item.title +:+ item.description +:+ item.amount + < br />;
}
}
}
}
希望这将有助于将来的其他人。
I am writing a simple XML parser which would pass this XML output: http://www.cpalead.com/dashboard/reports/campaign_rss.php?id=187000
The full C# code is:
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>";
}
}
offers is a div element.When I run this code I get an "System.Xml.XmlException: Invalid character in the given encoding. Line 8271, position 163." errorAs you can see I also set Encoding using ourBlog.Declaration.Encoding = "";I have tried:
- ISO-8859-1
- UTF-8
- windows-1251
- windows 1252
- UTF-16
I don't know what else to try.Do you have any suggestions?
EDIT:
Stack Trace is:
Source Error:
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";
Stack Trace:
[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
Ok, I have found a working solution here.
Here is the new code:
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 />";
}
}
}
}
Hope this will help somebody else in the future.
这篇关于System.Xml.XmlException:给定编码中的无效字符。线8271,位置163的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!