c# - 如何在没有容器的情况下反序列化2个子元素#

How to deserialize 2 child elements without the container in c#

我有以下xml并且有反序列化的需求

LockerBank xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ColumnList>
    <LockersColumn newPage="false">
      <LockersList>
        <LockerInfo>
          <Name>D08.001</Name>
          <Size>Medium</Size>
          <State>Available</State>
          <Terminal>false</Terminal>
        </LockerInfo>
        <LockerInfo>
          <Name>D08.002</Name>
          <Size>Medium</Size>
          <State>Available</State>
          <Terminal>false</Terminal>
        </LockerInfo>
        <LockerInfo>
          <Name>D08.003</Name>
          <Size>Medium</Size>
          <State>Available</State>
          <Terminal>false</Terminal>
        </LockerInfo>
      </LockersList>
      <Width>0</Width>
    </LockersColumn>
    <Section name="A">
      <LockersColumn newPage="false">
        <LockersList>
          <LockerInfo>
            <Name>D08.001</Name>
            <Size>Medium</Size>
            <State>Available</State>
            <Terminal>false</Terminal>
          </LockerInfo>
          <LockerInfo>
            <Name>D08.002</Name>
            <Size>Medium</Size>
            <State>Available</State>
            <Terminal>false</Terminal>
          </LockerInfo>
          <LockerInfo>
            <Name>D08.003</Name>
            <Size>Medium</Size>
            <State>Available</State>
            <Terminal>false</Terminal>
          </LockerInfo>
        </LockersList>
        <Width>0</Width>
      </LockersColumn>
      <LockersColumn newPage="false">
        <LockersList>
          <LockerInfo>
            <Name>D08.004</Name>
            <Size>Medium</Size>
            <State>Available</State>
            <Terminal>false</Terminal>
          </LockerInfo>
          <LockerInfo>
            <Name>D08.005</Name>
            <Size>Medium</Size>
            <State>Available</State>
            <Terminal>false</Terminal>
          </LockerInfo>
          <LockerInfo>
            <Name>D08.006</Name>
            <Size>Medium</Size>
            <State>Available</State>
            <Terminal>false</Terminal>
          </LockerInfo>
        </LockersList>
        <Width>0</Width>
      </LockersColumn>
    </Section>
</ColumnList>

</LockerBank>

我想反序列化如下

 [XmlRoot("LockerBank")]
public class TestBank
{
    public TestBank()
    {
        TerminalSize = 4;
    }

    [XmlArray(ElementName = "ColumnList")]
    [XmlArrayItem(Type = typeof(LockerColumnLayout), ElementName = "LockersColumn", IsNullable = true)]

    public List<LockerColumnLayout> Columns { get; set; }

    [XmlArray(ElementName = "ColumnList")]
    [XmlArrayItem(Type = typeof(SectionLayout), ElementName = "Section", IsNullable = true)]
    public SectionLayout[] SectionCollection { get; set; }

    [XmlAttribute("TerminalSize")]
    public int TerminalSize { get; set; }
}
    public class SectionLayout
{
    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlElement(Type = typeof(LockerColumnLayout), ElementName = "LockersColumn")]
    public LockerColumnLayout[] LockersColumn { get; set; }

}

public class LockerColumnLayout
{
    [XmlArray(ElementName = "LockersList"),
    XmlArrayItem(Type = typeof(LockerLayout), ElementName = "LockerInfo")]
    public LockerLayout[] Lockers { get; set; }

    [XmlAttribute("newPage")]
    public bool NewPage { get; set; }
}

public class LockerLayout
{
    public LockerLayout()
    {
        ColumnSpan = LockerSize.U1;
    }

    public string Name { get; set; }

    public LockerSize Size { get; set; }

    public LockerSize ColumnSpan { get; set; }

    [XmlIgnore]
    public bool IsTerminal
    {
        get
        {
            return this.Size == LockerSize.Terminal;
        }
    }

}

我收到一条错误消息“{"The XML element 'ColumnList' from namespace '' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element."}”

我如何反序列化它?

<ColumnList>可以包含多个<LockersColumn><Section>类型的子元素混合在一起,所以需要反序列化为共享基class的多态数组-- 在这种情况下,object:

[XmlRoot("LockerBank")]
public class TestBank
{
    public TestBank()
    {
        TerminalSize = 4;
    }

    [XmlArray(ElementName = "ColumnList")]
    [XmlArrayItem(Type = typeof(LockerColumnLayout), ElementName = "LockersColumn", IsNullable = true)]
    [XmlArrayItem(Type = typeof(SectionLayout), ElementName = "Section", IsNullable = true)]
    public List<object> LockerAndSectionLayouts { get; set; }

    [XmlIgnore]
    public IEnumerable<LockerColumnLayout> Columns { get { return (LockerAndSectionLayouts ?? Enumerable.Empty<object>()).OfType<LockerColumnLayout>(); } }

    [XmlIgnore]
    public IEnumerable<SectionLayout> SectionCollection { get { return (LockerAndSectionLayouts ?? Enumerable.Empty<object>()).OfType<SectionLayout>(); } }

    [XmlAttribute("TerminalSize")]
    public int TerminalSize { get; set; }
}

我添加了几个属性,它们返回列表中每种类型对象的过滤枚举。