开发者

XML deserialization: Deserialize missing element into null property value

My xml document has a element that can contains multiple child elements. In my class, I declare the property 开发者_开发技巧as:

[XmlArray("files", IsNullable = true)]
[XmlArrayItem("file", IsNullable = false)]
public List<File> Files { get; set; }

During deserialization, if the <files> element is missing, I want the Files property to be null. However, what happens is that Files is deserialized into an empty List object. How do I prevent that?


One option that achieves that is encapsulation of the list:

public class Foo
{
    [XmlElement("files", IsNullable = true)]
    public FooFiles Files { get; set; }

}
public class FooFiles
{
    [XmlElement("file", IsNullable = false)]
    public List<File> Files { get; set; }
}

Here, Foo.Files will be null if there is no <files/> element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜