我有以下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;
}
}
}
我收到错误说&#34; {&#34; XML元素&#39; ColumnList&#39;来自命名空间&#39;&#39;已存在于当前范围内。使用XML属性为元素指定另一个XML名称或命名空间。&#34;}&#34;
我如何反序列化?
答案 0 :(得分:0)
<ColumnList>
可以包含多个类型<LockersColumn>
和<Section>
混合在一起的子元素,因此需要将其反序列化为共享基类的多态数组 - 在这种情况下, 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; }
}
我添加了几个属性,返回列表中每种类型对象的已过滤枚举。