如何修复不正确序列化的对象列表

时间:2019-03-29 17:58:32

标签: c# xml serialization

我一直无法正确序列化对象。我有一个对象SensorSession看起来像:



    [XmlRoot("Session")]
    public class SensorSession
    {
        [XmlElement("SensorDefinitionCollection")]
        public SensorDefinitionCollection mCollection { get; set; }
        [XmlElement("SensorRunner")]
        public SensorRunner mRunner { set; get; }
    }

    public class SensorDefinitionCollection : IEnumerable<SensorDefinition>
    {
        [XmlArray("SensorDefinitionCollection")]
        [XmlArrayItem("SensorDefinition")]
        public List<SensorDefinition> mCollection;
    }
    public class SensorDefinition : IEquatable<SensorDefinition>, IComparable<SensorDefinition>
    {
        [XmlElement("StartingWavelength")]
        public double startingWavelength { get; set; }
        [XmlElement("StoppingWavelength")]
        public double stoppingWavelength { get; set; }
        [XmlElement("TargetWavelength")]
        public double targetWavelength { get; set; }

    }

序列化时,我得到以下信息:

<?xml version="1.0" encoding="utf-8"?>
<Session xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SensorDefinitionCollection>
    <StartingWavelength>1300</StartingWavelength>
    <StoppingWavelength>1400</StoppingWavelength>
    <TargetWavelength>1350</TargetWavelength>
  </SensorDefinitionCollection>
  <SensorDefinitionCollection>
    <StartingWavelength>1200</StartingWavelength>
    <StoppingWavelength>1300</StoppingWavelength>
    <TargetWavelength>1250</TargetWavelength>
  </SensorDefinitionCollection>
  <SensorRunner>
    <SensorConfig>
      <SampleCount>5</SampleCount>
      <SampleDelay>5</SampleDelay>
      <SampleTolerance>5</SampleTolerance>
    </SensorConfig>
  </SensorRunner>
</Session>

但是我希望得到的是:

<?xml version="1.0" encoding="utf-8"?>
<Session xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SensorDefinitionCollection>
    <SensorDefinition>
        <StartingWavelength>1200</StartingWavelength>
        <StoppingWavelength>1300</StoppingWavelength>
        <TargetWavelength>1250</TargetWavelength>
    </SensorDefinition>
  </SensorDefinitionCollection>
  <SensorRunner>
    <SensorConfig>
      <SampleCount>5</SampleCount>
      <SampleDelay>5</SampleDelay>
      <SampleTolerance>5</SampleTolerance>
    </SensorConfig>
  </SensorRunner>
</Session>

我如何允许列表(SensorDefinitionCollection)成为单个元素,并且该列表中的所有SensorDefinition都位于其中?

更新:

该问题已通过一些重构得以解决。该对象不是让集合对象包含列表,而是继承了带有定义对象的List。

    [XmlRoot("Session")]
    public class SensorSession
    {
        [XmlArray("SensorDefinitionCollection")]
        [XmlArrayItem("SensorDefinition")]
        public SensorDefinitionCollection Collection { get; set; }
        [XmlElement("SensorRunner")]
        public SensorRunner mRunner { set; get; }
    }

    [Serializable()]
    public class SensorDefinitionCollection : List<SensorDefinition>
    {
    }

    public class SensorDefinition : IEquatable<SensorDefinition>, IComparable<SensorDefinition>
    {
        [XmlElement("StartingWavelength")]
        public double startingWavelength { get; set; }
        [XmlElement("StoppingWavelength")]
        public double stoppingWavelength { get; set; }
        [XmlElement("TargetWavelength")]
        public double targetWavelength { get; set; }

    }

1 个答案:

答案 0 :(得分:0)

这应该可以帮助您

[XmlRoot(ElementName = "SensorDefinition")]
public class SensorDefinition
{
    [XmlElement(ElementName = "StartingWavelength")]
    public int StartingWavelength { get; set; }
    [XmlElement(ElementName = "StoppingWavelength")]
    public int StoppingWavelength { get; set; }
    [XmlElement(ElementName = "TargetWavelength")]
    public int TargetWavelength { get; set; }
}

[XmlRoot(ElementName = "SensorDefinitionCollection")]
public class SensorDefinitionCollection
{
    [XmlElement(ElementName = "SensorDefinition")]
    public List<SensorDefinition> SensorDefinition { get; set; }
}

[XmlRoot(ElementName = "SensorConfig")]
public class SensorConfig
{
    [XmlElement(ElementName = "SampleCount")]
    public int SampleCount { get; set; }
    [XmlElement(ElementName = "SampleDelay")]
    public int SampleDelay { get; set; }
    [XmlElement(ElementName = "SampleTolerance")]
    public int SampleTolerance { get; set; }
}

[XmlRoot(ElementName = "SensorRunner")]
public class SensorRunner
{
    [XmlElement(ElementName = "SensorConfig")]
    public List<SensorConfig> SensorConfig { get; set; }
}

[XmlRoot(ElementName = "Session")]
public class SensorSession
{
    [XmlElement(ElementName = "SensorDefinitionCollection")]
    public List<SensorDefinitionCollection> SensorDefinitionCollection { get; set; }
    [XmlElement(ElementName = "SensorRunner")]
    public List<SensorRunner> SensorRunner { get; set; }
}