【问题标题】:Duplicate 'System.SerializableAttribute' attribute when "paste xml as classes" for the same root tag相同根标签的“将 xml 粘贴为类”时重复“System.SerializableAttribute”属性
【发布时间】:2020-12-11 06:08:08
【问题描述】:

假设我有 2 个具有相同根的 XML,示例:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>   
</catalog>

在第二个 XML 文件中:

<?xml version="1.0"?>
<catalog>
   <author id="23">
      <author>Gambardella, Matthew</author>
      <age>45</age>
   </author>
   <author id="26">
      <author>Ali</author>
      <age>65</age>
   </author>       
</catalog>

当我想使用 Visual Studio 对其进行反序列化时,我将为每个 XML 创建一个类,复制 XML 内容并粘贴特殊 >“将 XML 粘贴为类”。

然后,我会得到很多为我生成的代码,如下所示:

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class catalog
    {

        private catalogBook[] bookField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("book")]
        public catalogBook[] book
        {
            get
            {
                return this.bookField;
            }
            set
            {
                this.bookField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class catalogBook
    {

        private string authorField;

        private string titleField;

        private string genreField;

        private decimal priceField;

        private System.DateTime publish_dateField;

        private string descriptionField;

        private string idField;

        /// <remarks/>
        public string author
        {
            get
            {
                return this.authorField;
            }
            set
            {
                this.authorField = value;
            }
        }

        /// <remarks/>
        public string title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }

        /// <remarks/>
        public string genre
        {
            get
            {
                return this.genreField;
            }
            set
            {
                this.genreField = value;
            }
        }

        /// <remarks/>
        public decimal price
        {
            get
            {
                return this.priceField;
            }
            set
            {
                this.priceField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
        public System.DateTime publish_date
        {
            get
            {
                return this.publish_dateField;
            }
            set
            {
                this.publish_dateField = value;
            }
        }

        /// <remarks/>
        public string description
        {
            get
            {
                return this.descriptionField;
            }
            set
            {
                this.descriptionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string id
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }
    }

问题是当我对第二个 XML 重复相同的步骤时,我会在这些行中得到一个错误:

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]

错误是Duplicate 'System.SerializableAttribute' attribute

我的问题是当我的 XML 文件具有相同的 ROOT 标签时如何解决这个问题?在这种情况下,根标签是catalog

【问题讨论】:

  • 注意:将 XML 粘贴为类选项非常方便,但生成的 C# 非常可怕。如果您愿意,可以用更简洁的手写类型来表示相同的东西,但是:如果这不重要,请不要担心

标签: c# xml


【解决方案1】:

现在,您在同一个 c# 命名空间中有两个 partial class catalog 定义。这意味着它们是组合的。两种选择:

  1. (首选)将它们放在不同的 c# 命名空间中——无论它们的用途有什么区别,都可以适当地命名
  2. 将它们留在原处,但重命名类型本身,包括任何其他类型(不仅仅是根)

或者,如果更有用的话,您可以拥有一个包含书籍和作者容器的 Catalog 类型。但是您必须手动编写类型。这样做可能需要大约 10-15 行代码(替换 所有 生成的代码)

【讨论】:

  • 但是我有 20 个 XML 文件。我想我会让 Visual Studio 自动生成代码。谢谢,有了不同的命名空间,现在没有更多错误了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多