Arturs Kirsis
Arturs Kirsis

Reputation: 129

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

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." error As you can see I also set Encoding using ourBlog.Declaration.Encoding = ""; I have tried:

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

Upvotes: 2

Views: 10309

Answers (2)

Arturs Kirsis
Arturs Kirsis

Reputation: 129

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.

Upvotes: 2

Codo
Codo

Reputation: 78795

Your XML file is indeed invalid. It's encoding is obviously UTF-8. But there's a problem on line 8271.

The line basically looks like this:

    <description>eMusic δίνει οπαδούς μουσικής της φανταστική συναλλάσσεται για μεγάλη μουσική, κατά μέσο όρο περίπου τα μισά από Amazon ή το iTunes κατάστημα. Έναρξ_</description>

But right before the closing tag where I put the underscore, the data seems to be cut off in the middle of a UTF-8 multi-byte character. In hex it looks like this:

CF 81 CE BE CE 3C 2F 64 65

CF 81 CE BE are the Greek letters ρξ, 3C 2F 64 65 is </de. But the remaining CE starts a multi-byte sequence that is cut off. The value is cut off at 255 bytes!

You need to fix the source file. It's invalid. And 255 bytes isn't a random length in IT. Probably more data is missing.

Upvotes: 0

Related Questions