簡體   English   中英

System.Xml.XmlException:給定編碼中的無效字符。 8271行,位置163

[英]System.Xml.XmlException: Invalid character in the given encoding. Line 8271, position 163

我正在編寫一個簡單的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 =“”;設置了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

好的,我在這里找到了可行的解決方案。

這是新代碼:

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 />"; 
                }
            }
        }
    }

希望這會在將來對其他人有所幫助。

您的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個字節!

您需要修復源文件。 無效 255個字節不是IT中的隨機長度。 可能丟失了更多數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM