XML 反序列化 <rss xmlns=''> 不是预期的

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

其中的另一个....

我查看了 Stack Overflow 上的许多其他示例,但没有找到使这项工作可行的解决方案。

错误:

    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% 确定格式正确):

<?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> 
    ....

可执行代码:

     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);
          }
     }

这东西快要死了,我已经研究了几个小时了。

有趣的是,我通常使用的工具是将我的 XML 和过去的特殊内容复制到 VS Studio 中,然后查看 class VS Studio 会自动构建以反序列化它。在这种情况下,它与我的代码完全相同。非常混淆。

由于 utf-16 出现错误。只需从 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);
               }
        }

问题是命名空间之一。 XML 中的 rss 元素位于默认命名空间中,但 googleRss 上的 XmlRoot 属性的命名空间为 http://base.google.com/ns/1.0.

xmlns:g="..." 的命名空间声明将命名空间绑定到前缀 g,但这在您问题的 XML 片段中的任何地方都没有使用。

XmlRoot 属性中删除 Namespace 值:

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

并从序列化器构造函数中删除 rss 的默认命名空间:

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

有关演示,请参阅 this fiddle