我有一個對象:Xmlserializer nillable元素與xmlattribute,可能嗎?
class Thing {
[Xmlarray("Widget", IsNullable=true) ]
List<Widget> Widgets;
}
class Widget {
[Xmlattribute]
public string Name;
[XmlTextAttribute]
public string Value;
}
基本上我想樣本輸出的樣子:
<Thing>
<Widget name="foo" xsi:nil="true"/>
<Widget name="bar">Nerds</Widget>
</Thing>
我遇到的問題是,XmlSerializer的不這樣做,爲Foo線。它不會爲包含null的小部件寫出xsi:nil
位。這只是一個空的元素(<Widget name="foo"/>
該結束了吃這個XML是老了,垃圾和在我的掌握,希望其零位在那裏,如果我想要一個小部件記錄從系統中刪除解析器/存儲與將其設置爲空(這是它在空的Widget條目中缺少零位時的作用)
對不起,如果出現錯誤,將它寫在手機上基本上我該如何讓xmlserializer寫入零位?
UPDATE:這裏是實際的標籤,我正在閱讀有關如何不能設置nillable如果有一個屬性在arrayitem上(Widgets列表中的Widget)。
<Widget xsi:nil="true"/>
對我沒用,因爲我已經提到的 - 項需要name屬性和零=真(它告訴處理器「這一領域,從商店中刪除」)。沒有名稱屬性,它不知道什麼字段。可悲的是,它只依賴於xsi:nil來告訴它。如果它看到一個空的<Widget name="foo"/>
- 它將其設置爲空/空,而不是完全刪除。
class Thing{
[System.Xml.Serialization.XmlArrayItemAttribute("Widget", IsNullable=true)]
public List<Widget> Widgets { get; set; }
}
class Widget{
[System.Xml.Serialization.XmlAttribute][JsonProperty]
public string name {get;set;}
[System.Xml.Serialization.XmlTextAttribute]
public string Value {get;set;}
}
本質上講,它不可能是<Widget name="foo"><Value>Bar</Value></Widget>
或<Widget xsi:nil=true/>
或<Widget name="foo"/>
- 必須是唯一的<Widget name="foo" xsi:nil="true"/>
。責備這件事發送到處理器(我無法控制)。
那麼,它是可序列化的嗎?
我可能寫錯了Xmlarray屬性。這是一個序列號提示,確實接受IsNullable。不幸的是我無法改變最終結果。它被餵給一個糟糕的老式和蹩腳的處理器,並不總是正確的。 – Jason 2014-12-04 12:42:54
當我進入辦公室並關閉手機時,我會編輯帖子以更清晰並提供我的問題的鏈接。 – Jason 2014-12-04 12:47:05
我已更新帖子以包含更好的信息。 – Jason 2014-12-04 15:50:29