简体   繁体   中英

XML Deserialize <rss xmlns=''> was not expected

Another one of these....

I've looked through many other examples on Stack Overflow and have not found a solution that makes this work.

Error:

    There is an error in XML document (1, 41).
System.Xml
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
   at CatalogInterface_1_1.MainWindow.cmdConvertToGoogle_Click(Object sender, RoutedEventArgs e) in C:\Users\Jamie.Marshall\Documents\Visual Studio 2015\Projects\CatalogInterface_1_1\CatalogInterface_1_1\MainWindow.xaml.cs:line 239
<rss xmlns=''> was not expected.

Object Class:

public class GCatalog
{
    [XmlType(AnonymousType = true)]
    [XmlRoot(ElementName = "rss", Namespace = "http://base.google.com/ns/1.0")]
    public partial class googleRss
    {
        [XmlAttribute("Version")]
        public string Version { get; set; }
        [XmlElement("channel")]
        public rssChannel Channel { get; set; }
    }
    [XmlType(AnonymousType = true)]
    public partial class rssChannel
    {
        public string title { get; set;  }
        public string link { get; set; }
        public string description { get; set; }
        [XmlElement("Item")]
        public Page Items { get; set; }
    }
    [XmlRoot(ElementName = "rss", Namespace = "http://base.google.com/ns/1.0")]
    public class Page : List<Item> { }
    [XmlType("Item")]
    public class Item
    {
        #region private properties
        private props.....
        #endregion

        #region public propterties
        [XmlElement("id", Namespace = "http://base.google.com/ns/1.0")]
        public string id {get; set; }
        [XmlElement("availabilityid", Namespace = "http://base.google.com/ns/1.0")]
        public string availability {get; set; }
        [XmlElement("condition", Namespace = "http://base.google.com/ns/1.0")]
        public string condition {get; set; }
        [XmlElement("description", Namespace = "http://base.google.com/ns/1.0")]
        public string description {get; set; }
        [XmlElement("image_link", Namespace = "http://base.google.com/ns/1.0")]
        public string image_link {get; set; }
        [XmlElement("link", Namespace = "http://base.google.com/ns/1.0")]
        public string link {get; set; }
        [XmlElement("title", Namespace = "http://base.google.com/ns/1.0")]
        public string title {get; set; }
        [XmlElement("price", Namespace = "http://base.google.com/ns/1.0")]
        public string price {get; set; }
        [XmlElement("brand", Namespace = "http://base.google.com/ns/1.0")]
        public string brand {get; set; }
        [XmlElement("identifier_exists", Namespace = "http://base.google.com/ns/1.0")]
        public string identifier_exists {get; set; }
        [XmlElement("additional_image_link", Namespace = "http://base.google.com/ns/1.0")]
        public string additional_image_link {get; set; }
        [XmlElement("google_product_category", Namespace = "http://base.google.com/ns/1.0")]
        public string google_product_category {get; set; }
        [XmlElement("product_type", Namespace = "http://base.google.com/ns/1.0")]
        public string product_type {get; set; }
        [XmlElement("sale_price", Namespace = "http://base.google.com/ns/1.0")]
        public string sale_price {get; set; }
        [XmlElement("sale_price_effective_date", Namespace = "http://base.google.com/ns/1.0")]
        public string sale_price_effective_date {get; set; }
    }
}

XML Header (100% sure is well formed):

<?xml version="1.0" encoding="utf-16"?>
<rss xmlns:g="http://base.google.com/ns/1.0" Version="2.0">
  <channel>
    <title>CatalogFB</title>
    <link>https://store.playstation.com/#!/en-us</link>
    <description>All Items in Catalog</description>
    <Item> 
    ....

Executable Code:

     FBCatalog.googleRss dataObject = new FBCatalog.googleRss();
     using (XmlReader reader = XmlReader.Create(new StringReader(xml.InnerXml)))
     {
          var serializer = new XmlSerializer(typeof(FBCatalog.googleRss), "rss");
          dataObject = (FBCatalog.googleRss)serializer.Deserialize(reader);
          GCatalog.Page page = new GCatalog.Page();
          counter = 0;

          foreach (var ITEM in dataObject.Channel.Items)
          {
              GCatalog.Item gItem = GCatalog.ConvertToGItem(ITEM);
              page.Add(gItem);
          }
     }

This thing is killing me, I've been on it for hours.

The funny thing is my usual go to tool is to copy my XML and past special into VS Studio and look at the class VS Studio would automatically build to deserialize this. In this instance, its the exact same as my code. Very confusing.

The issue is one of namespaces. The rss element in your XML is in the default namespace, but the XmlRoot attribute on googleRss has a namespace of http://base.google.com/ns/1.0 .

The namespace declaration of xmlns:g="..." binds a namespace to the prefix g , but this isn't used anywhere in the snippet of XML in your question.

Remove the Namespace value from the XmlRoot attribute:

[XmlRoot(ElementName = "rss")]
public partial class googleRss

And remove the default namespace of rss from the serializer constructor:

var serializer = new XmlSerializer(typeof(GCatalog.googleRss));

See this fiddle for a demo.

Error is occurring due to utf-16. Simply skip line by reading one line from the StringReader.

         FBCatalog.googleRss dataObject = new FBCatalog.googleRss();
         using (StringReader sReader = new StringReader(xml.InnerXml))
         {
              sReader.ReadLine();
              XmlReader reader = XmlReader.Create(sReader);
              var serializer = new XmlSerializer(typeof(FBCatalog.googleRss), "rss");
          dataObject = (FBCatalog.googleRss)serializer.Deserialize(reader);
          GCatalog.Page page = new GCatalog.Page();
          counter = 0;
          foreach (var ITEM in dataObject.Channel.Items)
               {
                   GCatalog.Item gItem = GCatalog.ConvertToGItem(ITEM);
                   page.Add(gItem);
               }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM