2012-02-20 75 views
2

我有我想要序列马克现场为具有属性IsRequired [的XmlElement]

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://xyz.com/schema")] 
public class Customer 
{ 
    [System.Xml.Serialization.XmlElementAttribute(Order = 0)] 
    public int Id { get; set; } 

    [System.Xml.Serialization.XmlElementAttribute(Order = 1)] 
    public string Name { get; set; } 

    [System.Xml.Serialization.XmlElementAttribute(Order = 2)] 
    public string Url{ get; set; } 

    [System.Xml.Serialization.XmlElementAttribute(Order = 3)] 
    public string Count { get; set; } 
} 

反序列化时,如果地址为null,则出XML没有包含URL节点的类。 如何将它标记为必填字段,以便每次都创建Url节点?

+4

你为什么不使用'使用的System.Xml.Serialization;'和删除所有空间前缀? – svick 2012-02-20 10:50:17

回答

2

IsNullable属性应该这样做。

[System.Xml.Serialization.XmlElementAttribute(Order = 2,IsNullable=true)] 

如果它设置为true,它应该生成标记(根据MSDN)。

如果IsNullable属性设置为true,则xsi:nil属性是已设置为空引用 (在Visual Basic中为Nothing)类成员产生 。例如,如果您将名为 MyStringArray的字段设置为null引用(在Visual Basic中为Nothing),则 XmlSerializer会生成以下XML代码。

<MyStringArray xsi:nil = "true" /> 
相关问题