2011-10-28 21 views
5

我有以下XML結構。該theElement元素可以包含theOptionalList元素,或不:如何裝飾/定義類成員與XmlSerializer的使用可選的XML元素?

<theElement attrOne="valueOne" attrTwo="valueTwo"> 
    <theOptionalList> 
     <theListItem attrA="valueA" /> 
     <theListItem attrA="anotherValue" /> 
     <theListItem attrA="stillAnother" /> 
    </theOptionalList> 
</theElement> 
<theElement attrOne="anotherOne" attrTwo="anotherTwo" /> 

什麼是表達對應的類結構的清潔方法是什麼?

我敢肯定如下:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml.Serialization; 

namespace MyNamespace 
{ 
    public class TheOptionalList 
    { 
     [XmlAttributeAttribute("attrOne")] 
     public string AttrOne { get; set; } 

     [XmlAttributeAttribute("attrTwo")] 
     public string AttrTwo { get; set; } 

     [XmlArrayItem("theListItem", typeof(TheListItem))] 
     public TheListItem[] theListItems{ get; set; } 

     public override string ToString() 
     { 
      StringBuilder outText = new StringBuilder(); 

      outText.Append("attrOne = " + AttrOne + " attrTwo = " + AttrTwo + "\r\n"); 

      foreach (TheListItem li in theListItems) 
      { 
       outText.Append(li.ToString()); 
      } 

      return outText.ToString(); 
     } 
    } 
} 

除了:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml.Serialization; 

namespace MyNamespace 
{ 
    public class TheListItem 
    { 
     [XmlAttributeAttribute("attrA")] 
     public string AttrA { get; set; } 

     public override string ToString() 
     { 
      StringBuilder outText = new StringBuilder(); 

      outText.Append(" attrA = " + AttrA + "\r\n");     
      return outText.ToString(); 
     } 
    } 
} 

但是有關theElement?難道我拿theOptionalList元素爲數組類型有它讀什麼它找到的文件(無論是什麼,或一個)中,然後在代碼檢查它是否是有或沒有?還是有另一個裝飾者,我可以提供?還是隻是工作?

編輯:我結束了使用從this answer信息。

回答

5

嘗試增加IsNullable = trueXmlArrayItem屬性。

+0

感謝詹姆斯的迴應。我猜這將處理一個沒有元素的'TheOptionalList'元素,但是它會處理'根本不存在的'OptionalList'? (我可能不明白你的建議。)另外,我的格式沒有模式;這完全取決於我和其他開發者之間的口頭協議。:) – John

+0

我不積極,但我認爲使用'IsNullable'會排除它,如果數組爲空。 –

+1

考慮一個模式。它非常有用,無論多麼小,對你的'工具包'來說都是一件好事。 – Gusdor

4

看起來你可以使用另一個布爾指定要包括的元素或沒有。

另一種方法是使用一種特殊的模式創建由XmlSerializer的認可布爾字段 ,並向XmlIgnoreAttribute 適用於該領域。該模式以 propertyNameSpecified的形式創建。例如,如果有一個名爲場 「MyFirstName」你也將創建一個名爲 「MyFirstNameSpecified」指示XmlSerializer的是否 生成一個名爲「MyFirstName」的XML元素領域。以下示例顯示在 中。

public class OptionalOrder 
{ 
    // This field should not be serialized 
    // if it is uninitialized. 
    public string FirstOrder; 

    // Use the XmlIgnoreAttribute to ignore the 
    // special field named "FirstOrderSpecified". 
    [System.Xml.Serialization.XmlIgnoreAttribute] 
    public bool FirstOrderSpecified; 
} 

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

+0

感謝您的響應的方法。這個例子處理序列化,這是我面臨的一部分。另一部分是反序列化。如果我在這種格式的文件上調用Deserialize()會怎麼樣?因爲我不認爲我可以告訴'XmlSerializer'作爲整個文件的每個元素。換句話說,如果我反序列化並且沒有看到類中指定的成員,是否存在默認行爲? – John

+0

是的,有一個默認行爲:類的默認構造函數。當您反序列化XML時,XMLSerializer會調用默認構造函數來創建對象,因此您可以在此設置默認值。這是通常有助於在默認的構造器的屬性設置默認值和追加':這()'您的其他構造,以確保您的默認值被首先設置。 – JCH2k

0

附加到XxySpecifed屬性,還存在與ShouldSerialize前綴

[XmlElement] 
public List<string> OptionalXyz {get; set;} 

public bool ShouldSerializeOptionaXyz() { 
    return OptionalXyz != null && OptionalXyz.Count > 0 ; 
}