簡體   English   中英

Xml序列化基類屬性

[英]Xml serialization Base class attributes

我使用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>

解決方案是將“ Attr1Specified”值指定為true。 這是答案。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM