【发布时间】:2014-12-05 11:15:50
【问题描述】:
我有一个对象:
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 行执行此操作。对于 Value 包含 null 的小部件,它不会写出 xsi:nil 位。它只是一个空元素 (<Widget name="foo"/>
最终吃掉这个 Xml 的解析器很旧,而且很垃圾,而且不受我的控制。如果我希望将小部件记录从其系统/存储中删除而不是将其设置为空(如果空小部件条目中缺少 nil 位,则它会执行此操作),它希望 nil 位存在。
如有错误,请在手机上写。本质上,我如何让 xmlserializer 写入 nil 位?
更新:这是实际的标签。我正在阅读关于如果 arrayitem 上有一个属性(小部件列表中的小部件)如何不能设置为 nillable 的模糊内容。
<Widget xsi:nil="true"/>
对我来说没用,正如我所提到的——条目需要 name 属性和 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"/>。责怪这个东西被发送到的处理器(我无法控制)。
那么,它是可序列化的吗?
【问题讨论】:
标签: c# .net xmlserializer