使用 xsi:nil="true" C# 序列化删除 xml 元素

sin*_*123 4 c# xml serialization xmlserializer

我有一个 XML,其中包含一些值,有时可能存在空值,如下所示: 我根本不希望在 XML 中列出带有 null 的节点!元素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>
Run Code Online (Sandbox Code Playgroud)

类结构如下:

[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; }
}
Run Code Online (Sandbox Code Playgroud)

dbc*_*dbc 7

基本问题是你已经设置了XmlElementAttribute.IsNullable = true。来自文档

获取或设置一个值,该值指示XmlSerializer是否必须序列化设置null为空标记且xsi:nil属性设置为 true 的成员。

因此,理论上您可以将其设置为 false(或根本不设置),并且xsi:nil不会为 null 值发出属性:

[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; }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果这样做,您可能会在反序列化遗留 XML 文件时遇到问题,即字符串值xsi:nil元素现在将被反序列化为空字符串而不是null字符串。(此处演示小提琴#1 。)如果这成为问题,您必须保留该设置,而使用ShouldSerialize*() 与 *Specified Conditional Serialization Pattern的答案XmlElementAttribute.IsNullable = true描述的条件序列化模式之一:

[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; }
}
Run Code Online (Sandbox Code Playgroud)

完成此操作后,<NodeName xsi:nil="true" />元素将在反序列化期间映射到null字符串,并在序列化期间完全跳过。

笔记:

  • 您的 XML 格式不正确。它缺少名称空间前缀的定义xsi:,可能是因为它是某个较大文档的片段。这个答案假设应该是:

    <Materials 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    Run Code Online (Sandbox Code Playgroud)
  • 有些元素根本不绑定到您的 C# 数据模型,包括:

    <V5_Weight>2.009</V5_Weight>
    <V6_Weight>1.3318154561904</V6_Weight>
    
    Run Code Online (Sandbox Code Playgroud)

    <V5_density>1000</V5_density>
    <V6_density>663</V6_density>
    
    Run Code Online (Sandbox Code Playgroud)

    这个答案并不试图解决这个问题(如果它实际上是一个问题)。

示例小提琴#2在这里