如果您只是想丢弃xsi:nil
属性、集合XmlElementAttribute.IsNullable = false https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute.isnullable.aspx in the [XmlElement]
引用的包含类中的属性上的属性location
。
例如。如果你有
[System.Xml.Serialization.XmlRootAttribute("locations", Namespace = "")]
public class LocationList
{
[XmlElement("location", IsNullable = true)]
public List<location> Locations { get; set; }
}
手动修改为:
[System.Xml.Serialization.XmlRootAttribute("locations", Namespace = "")]
public class LocationList
{
[XmlElement("location", IsNullable = false)]
public List<location> Locations { get; set; }
}
如果你想bind的值xsi:nil
同时绑定到其他属性值,除了设置IsNullable = false
,您可以按照中所示的方式添加手动属性我可以在由 XSD C# 生成的类创建的 XML 中的同一标记处包含 null 属性和其他属性吗? https://stackoverflow.com/questions/32903839/can-i-have-null-attribute-and-other-attribute-at-the-same-tag-in-xml-created-by:
public partial class location
{
bool nil;
[XmlAttribute("nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public bool Nil
{
get
{
return nil;
}
set
{
nil = value;
}
}
public bool ShouldSerializeNil() { return nil == true; }
}
Sample fiddle https://dotnetfiddle.net/LQOZDQ.
您还可以对 XML 进行预处理和后处理以重命名xsi:nil
属性,如建议的那样这个答案 https://stackoverflow.com/questions/32903839/can-i-have-null-attribute-and-other-attribute-at-the-same-tag-in-xml-created-by/32969557#32969557.