我有一个XML,它有一些值,有时可以有空值,如下所示:我不希望XML中的所有节点都列在空中!这些元素在课堂上设置为IsNullable = true
。任何建议,因为我在谷歌尝试了很多东西..没有任何帮助!
<?xml version="1.0" encoding="utf-8"?>
<Materials>
<Material>
<MaterialName>ABC</MaterialName>
<Weight Value="0.303">
<Weight_A xsi:nil="true" />
<Weight_B xsi:nil="true" />
</Weight>
<Density Value="800">
<Density_A xsi:nil="true" />
<Density_B xsi:nil="true" />
</Density>
<Volume Value="8771.427" />
</Material>
<Material>
<MaterialName>ABC</MaterialName>
<Weight>
<V5_Weight>2.009</V5_Weight>
<V6_Weight>1.3318154561904</V6_Weight>
</Weight>
<Density>
<V5_density>1000</V5_density>
<V6_density>663</V6_density>
</Density>
<Volume Value="2008771.427" />
</Material>
</Materials>
课程结构如下:
[XmlRoot(ElementName = "Weight")]
public class Weight
{
[XmlElement(ElementName = "Weight_A", IsNullable = true)]
public string Weight_A { get; set; }
[XmlElement(ElementName = "Weight_B", IsNullable = true)]
public string Weight_B { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Density")]
public class Density
{
[XmlElement(ElementName = "Density_A", IsNullable = true)]
public string Density_A { get; set; }
[XmlElement(ElementName = "Density_B", IsNullable = true)]
public string Density_B { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Volume")]
public class Volume
{
[XmlElement(ElementName = "Volume_A")]
public string Volume_A { get; set; }
[XmlElement(ElementName = "Volume_B")]
public string Volume_B { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Material")]
public class Material
{
[XmlElement(ElementName = "MaterialName")]
public string MaterialName { get; set; }
[XmlElement(ElementName = "Weight")]
public Weight Weight { get; set; }
[XmlElement(ElementName = "Density")]
public Density Density { get; set; }
[XmlElement(ElementName = "Volume")]
public Volume Volume { get; set; }
}
[XmlRoot(ElementName = "Materials")]
public class Materials
{
[XmlElement(ElementName = "Material")]
public List<Material> Material { get; set; }
}
基本问题是你设置了XmlElementAttribute.IsNullable = true
。来自docs:
获取或设置一个值,该值指示XmlSerializer是否必须将设置为
null
的成员序列化为空标记,并将xsi:nil
属性设置为true。
因此,理论上你可以将它设置为假(或根本不设置它),并且不会为空值发出xsi:nil
属性:
[XmlRoot(ElementName = "Weight")]
public class Weight
{
[XmlElement(ElementName = "Weight_A" /*, IsNullable = true*/)]
public string Weight_A { get; set; }
[XmlElement(ElementName = "Weight_B" /*, IsNullable = true*/)]
public string Weight_B { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Density")]
public class Density
{
[XmlElement(ElementName = "Density_A"/*, IsNullable = true*/)]
public string Density_A { get; set; }
[XmlElement(ElementName = "Density_B"/*, IsNullable = true*/)]
public string Density_B { get; set; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
但是,如果这样做,您可能会遇到反序列化旧XML文件的问题,即字符串值xsi:nil
元素现在将被反序列化为空字符串而不是null
字符串。 (演示小提琴#1 here。)如果这成为一个问题,你必须保留XmlElementAttribute.IsNullable = true
设置,而是使用this answer中描述的条件序列化模式之一到ShouldSerialize*() vs *Specified Conditional Serialization Pattern:
[XmlRoot(ElementName = "Weight")]
public class Weight
{
[XmlElement(ElementName = "Weight_A", IsNullable = true)]
public string Weight_A { get; set; }
public bool ShouldSerializeWeight_A() { return Weight_A != null; }
[XmlElement(ElementName = "Weight_B", IsNullable = true)]
public string Weight_B { get; set; }
public bool ShouldSerializeWeight_B() { return Weight_B != null; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName = "Density")]
public class Density
{
[XmlElement(ElementName = "Density_A", IsNullable = true)]
public string Density_A { get; set; }
public bool ShouldSerializeDensity_A() { return Density_A != null; }
[XmlElement(ElementName = "Density_B", IsNullable = true)]
public string Density_B { get; set; }
public bool ShouldSerializeDensity_B() { return Density_B != null; }
[XmlAttribute(AttributeName = "Value")]
public string Value { get; set; }
}
完成此操作后,<NodeName xsi:nil="true" />
元素将在反序列化期间映射到null
字符串,并在序列化期间完全跳过。
笔记:
xsi:
名称空间前缀的定义,可能是因为它是某个较大文档的片段。这个答案假定它应该是:
<Materials
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<V5_Weight>2.009</V5_Weight>
<V6_Weight>1.3318154561904</V6_Weight>
和
<V5_density>1000</V5_density>
<V6_density>663</V6_density>
这个答案并没有试图解决这个问题(如果它实际上是一个问题)。样品小提琴#2 here。