2017-03-14 14 views
0

私はXSDから生成されたAC#クラスを持つの要素を追加していないシリアル化、私はそれをシリアル化しています、私はいくつかの要素が特定の条件に追加されるシナリオを持っている、これが私のクラスであるXML XML

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
public partial class MyRequestClass 
{ 
    private string testNOField; 
    private string mOBNOField; 
    private int sTYPEField; 
    private System.DateTime fRMDATEField; 
    private System.DateTime tODATEField; 
    public string TESTNO 
    { 
     get 
     { 
      return this.testNOField; 
     } 
     set 
     { 
      this.testNOField = value; 
     } 
    } 

    public string MOBNO 
    { 
     get 
     { 
      return this.mOBNOField; 
     } 
     set 
     { 
      this.mOBNOField = value; 
     } 
    } 


    public int STYPE 
    { 
     get 
     { 
      return this.sTYPEField; 
     } 
     set 
     { 
      this.sTYPEField = value; 
     } 
    } 


    [System.Xml.Serialization.XmlElementAttribute(DataType = "date")] 
    public System.DateTime FROMDATE 
    { 
     get 
     { 
      return this.fRMDATEField; 
     } 
     set 
     { 
      this.fRMDATEField = value; 
     } 
    } 


    [System.Xml.Serialization.XmlElementAttribute(DataType = "date")] 
    public System.DateTime TODATE 
    { 
     get 
     { 
      return this.tODATEField; 
     } 
     set 
     { 
      this.tODATEField = value; 
     } 
    } 

} 

プロパティFromDateとToDateが特定の条件で追加されます。問題は、これらの2つのプロパティに値を提供しているとき、XMLでこれらを追加していないとき、エラーが表示されないときです。 これは私が私は2つの異なるケースで、このクラスをシリアル化しています

 string output = string.Empty; 
     XmlSerializer xsSubmit = new XmlSerializer(typeof(MyRequestClass)); 
     using (StringWriter sww = new StringWriter()) 
      { 
       using (XmlWriter writer = XmlWriter.Create(sww)) 
       { 
        // sww.WriteLine(@"<?xml version=""1.0"" encoding=""UTF-8""?>"); 
        xsSubmit.Serialize(writer , env); 
        output = sww.ToString(); 
       } 
      } 

     var doc = XDocument.Parse(output); 
     Enumerable<XElement> emptyElements; 
     emptyElements = from descendant in doc.Descendants() 
         where descendant.IsEmpty || string.IsNullOrWhiteSpace(descendant.Value) 
         select descendant; 
     emptyElements.Remove(); 
     doc.Root.RemoveAttributes(); 
     output = doc.ToString(); 

第二には、1ケースでMOBNOは、XMLに表示され、その他の場合には、それは表示されません、私はこの機能を実装するために何をすべきシリアライズしています方法です?

答えて

0

属性値を動的に追加/変更できます。この場合、XmlIgnore(要素を非表示にする)になります。コードは次のとおりです。

if (yourCondition) 
{ 
    XmlAttributes myRequestClassPropertyAttributes = new XmlAttributes(); 
    myRequestClassPropertyAttributes.XmlIgnore = true; 

    XmlAttributeOverrides myRequestClassAttributes = new XmlAttributeOverrides(); 
    myRequestClassAttributes.Add(typeof(MyRequestClass), "MOBNO", myRequestClassPropertyAttributes); 

    XmlSerializer xsSubmit = new XmlSerializer(typeof(MyRequestClass), myRequestClassAttributes); 

    using (StringWriter sww = new StringWriter()) 
    { 
      using (XmlWriter writer = XmlWriter.Create(sww)) 
      { 
       // sww.WriteLine(@"<?xml version=""1.0"" encoding=""UTF-8""?>"); 
       xsSubmit.Serialize(writer, env); 
       output = sww.ToString(); 
      } 
     } 
}