【问题标题】:System.Xml.XmlException: Invalid character in the given encoding. Line 8271, position 163System.Xml.XmlException:给定编码中的字符无效。 8271 行,位置 163
【发布时间】:2014-01-05 22:18:08
【问题描述】:

我正在编写一个简单的 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>";
        }

    }

offers 是一个 div 元素。 当我运行此代码时,我得到一个“System.Xml.XmlException:给定编码中的无效字符。第 8271 行,位置 163。”错误 如您所见,我还使用 ourBlog.Declaration.Encoding = ""; 设置了编码。 我试过了:

  • ISO-8859-1
  • UTF-8
  • windows-1251
  • Windows 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

【问题讨论】:

  • 堆栈跟踪是什么?
  • 无论编码是什么,XML 中的某些字符都是非法的(例如,代码低于 32 的某些字符)-也许这是您的情况?
  • 如果XML中有非法字符,为什么我可以在IE和Chrome中打开呢?
  • 老兄,您可以在 IE 和 Chrome 中打开某些网站这一事实不能作为您代表有效 XML 的保证。使用您在本网站上展示的特定示例,您可以尝试任何 XML 验证器,它会简单地将其作为无效 XML 进行轰炸。因此,当您尝试在 XML 解析器中加载无效 XML 时,遇到异常是很正常的。
  • 我的朋友用 PHP 为同一页面编写了一个解析器,它工作得非常好。他所要做的就是将标头中的 content-type 设置为 UTF-8。这不是说 XML 没问题吗?

标签: c# xml encoding


【解决方案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 />"; 
                }
            }
        }
    }

希望这会在未来对其他人有所帮助。

【讨论】:

【解决方案2】:

您的 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 是&lt;/de。但是剩下的 CE 开始了一个被切断的多字节序列。该值在 255 字节处被截断!

您需要修复源文件。这是无效的。并且 255 字节在 IT 中不是随机长度。可能还有更多数据丢失。

【讨论】:

  • 问题是它不是我的文件。该文件由第 3 方提供。有什么办法可以跳过那一行而忽略它?
  • 您需要编写一个预处理器来检查有效的 UTF-8 字符并丢弃所有无效序列。如果你喜欢摆弄,这不是火箭科学。
  • 天啊,听起来像是一项复杂的任务..但是哦,好吧,别无选择,必须做一些谷歌搜索然后去做。感谢您的帮助。
  • 这里是一些检查有效 UTF-8 的 C 代码:snowplow.org/martin/utf8checker
  • 谢谢,但这并没有真正的帮助,因为我根本不懂 C。而且我也只是在学习 C#... 无论如何感谢您的帮助,不想再占用您的时间了。将尝试谷歌一些 C# 解决方案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
  • 2014-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多