使用xsi删除xml元素:nil =“true”C#serialization

问题描述 投票:2回答:1

我有一个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; }
}
c# xml serialization xmlserializer
1个回答
0
投票

基本问题是你设置了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字符串,并在序列化期间完全跳过。

笔记:

  • 您的XML格式不正确。它缺少xsi:名称空间前缀的定义,可能是因为它是某个较大文档的片段。这个答案假定它应该是: <Materials xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  • 有些元素根本没有绑定到您的c#数据模型,包括: <V5_Weight>2.009</V5_Weight> <V6_Weight>1.3318154561904</V6_Weight> <V5_density>1000</V5_density> <V6_density>663</V6_density> 这个答案并没有试图解决这个问题(如果它实际上是一个问题)。

样品小提琴#2 here

© www.soinside.com 2019 - 2024. All rights reserved.