我有一个XML,它具有一些值,有时可能会有null值,如下所示:我不希望XML中完全列出具有null的节点!元素在类中设置为IsNullable = true
。我在Google尝试了很多东西的任何建议。没有任何帮助!
<?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; }
}