【问题标题】:Xml serialization Base class attributesxml序列化基类属性
【发布时间】:2016-04-24 03:00:27
【问题描述】:

我使用 XmlSerializer 类来序列化继承基类“BaseClass”的派生类“MyContext”。序列化的 XML 输出中不存在基类属性“attr1”、“attr2”。我需要 Context 类和 Context 类中的嵌套类中的这些属性。请帮助这里缺少什么。

namespace MyConsoleApplication
{
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(MyContextType))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    public partial class BaseType
    {
     private MyEnumType attr1Field;
     private MyEnumType attr2Field;

     [System.Xml.Serialization.XmlAttributeAttribute()]
     public MyEnumType Attr1
        {
            get
            {
                return this.attr1Field;
            }
            set
            {
                this.attr1Field= value;
            }
        }

     [System.Xml.Serialization.XmlAttributeAttribute()]    
     public MyEnumType Attr2
        {
            get
            {
                return this.attr2Field;
            }
            set
            {
                this.attr2Field= value;
            }
        }
    }


     [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
     [System.SerializableAttribute()]
     [System.Diagnostics.DebuggerStepThroughAttribute()]
     [System.ComponentModel.DesignerCategoryAttribute("code")]
     [System.Xml.Serialization.XmlRootAttribute("Context", Namespace = "", IsNullable = false)]
     public partial class MyContextType : BaseType
     {
         private string element1Field;

         private string element2Field;

         [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
         public string Element1
         {
             get
             {
                 return this.element1Field;
             }
             set
             {
                 this.element1Field = value;
             }
         }
         [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
         public string Element2
         {
             get
             {
                 return this.element2Field;
             }
             set
             {
                 this.element2Field = value;
             }
         }
     }

     public enum MyEnumType
     {

         /// <remarks/>
         False,

         /// <remarks/>
         True,
     }
}

我的序列化代码:

var mydoc = new XmlDocument();
var context = new MyContextType() { Element1 = "Car", Element2 = "Bike", Attr1 = "id1", Attr2 = "id2" };  

using (MemoryStream stream = new MemoryStream())
    {
        XmlSerializer s = new XmlSerializer(typeof(MyContextType));
        s.Serialize(XmlWriter.Create(stream), context);
        stream.Flush();
        stream.Seek(0, SeekOrigin.Begin);
        mydoc.Load(stream);
    }

输出 Xml:

<MyContext>
    <Element1>Happy</Element1>
    <Element2>10</Element2>
</MyContext>

但我希望输出为

<MyContext attr1 = "False" attr2="False">
        <Element1>Happy</Element1>
        <Element2>10</Element2>
</MyContext>

【问题讨论】:

  • XmlSerializer 只会序列化公共类和成员。因为它是您的 MyContext 类,所以根本无法序列化。你能提供一个complete example你的问题吗?
  • 您的代码无法编译,并且您的类和属性未公开。一旦我对这些问题进行了合理的修复,我就无法重现您的问题。见dotnetfiddle.net/ueui7z
  • dbc:按要求更新了代码示例。请检查并告诉我
  • XmlAttributeAttribute 没有 Order 属性,因此代码无法编译。如果我解决了这个问题,我仍然无法重现该问题,请参阅dotnetfiddle.net/CWQZ1Y。请注意,您将Attr1 标记为[XmlElementAttribute] 而不是[XmlAttributeAttribute],因此Attr1 显示为元素而不是属性。 Attr2 虽然显示为属性。
  • 我已经修复了编译器错误并更新了我的帖子。实际上,我通过在 MyContext 对象中指定“Attr1Specified”值来填充属性,如下所示。 var context = new MyContextType() { Element1 = "Car", Element2 = "Bike", Attr1 = "id1", Attr1Specified = true, Attr2 = "id2", Attr2Specified = true };

标签: c# xmlserializer


【解决方案1】:

解决方案是将“Attr1Specified”值指定为 true。这是答案。

 var context = new MyContextType() { Element1 = "Car", Element2 = "Bike", Attr1 = MyEnumType.False, Attr1Specified = true, Attr2 = MyEnumType.False, Attr2Specified = true };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多