2016-04-24 17 views
0

Im XmlSerializerクラスを使用して、基底クラス "BaseClass"を継承する派生クラス "MyContext"を直列化します。直列化されたXML出力には、 "attr1"、 "attr2"という基本クラス属性はありません。これらの属性は、ContextクラスとContextクラス内の入れ子クラスで必要です。ここで欠けているものを手伝ってください。Xmlのシリアル化基底クラスの属性

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> 
+0

'XmlSerializer'は、パブリッククラスとメンバのみをシリアル化します。あなたの 'MyContext'クラスはまったく直列化できません。問題の[完全な例](https://stackoverflow.com/help/mcve)を提供してください。 – dbc

+0

コードはコンパイルされず、クラスとプロパティは公開されません。これらの問題の可能性のある修正を行うと、問題は再現できません。 – dbc

+0

を参照してください。dbc:要求されたコードサンプルを更新しました。チェックして私に知らせてください – KKR

答えて

0

として出力ソリューションは、 "Attr1Specified" を指定したたい値をtrueに設定します。ここに答えがあります。

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