【发布时间】:2020-11-18 21:33:54
【问题描述】:
我被困在如何解决这个 XML 结构的反序列化问题上。元素“allocatables”在同一个命名空间中有不同的类型。这些元素是否在“employeeAllocation”元素/对象中的一个或多个列表中反序列化并不重要。
这是 XML sn-p。
<ns3:EmployeeAllocationResponse
xmlns:ns3="http://employeeallocation.intf.mb.ivu.de/"
xmlns:ns2="http://employeeallocation.intf.mb.ivu.de/fault">
<employeeAllocation>
<allocatables
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns3:Duty">
<startTime>2020-11-03T14:09:00.000</startTime>
<endTime>2020-11-03T22:50:00.000</endTime>
<shortName>64280S</shortName>
</allocatables>
<allocatables
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns3:Duty">
<startTime>2020-11-03T14:09:00.000</startTime>
<endTime>2020-11-03T22:50:00.000</endTime>
<shortName>64280S</shortName>
</allocatables>
</employeeAllocation>
<employeeAllocation>
<allocatables
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns3:Absence">
<typeAbbreviation>STAG</typeAbbreviation>
<typeLongname>STAG-dagar</typeLongname>
<startTime>2020-11-03T00:00:00.000</startTime>
<endTime>2020-11-04T00:00:00.000</endTime>
<type>STAG</type>
</allocatables>
</employeeAllocation>
<employeeAllocation>
<allocatables
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns3:Duty">
<startTime>2020-11-03T21:00:00.000</startTime>
<endTime>2020-11-04T06:45:00.000</endTime>
<shortName>TPL1030</shortName>
<rosterAbbreviation>DLT</rosterAbbreviation>
</allocatables>
</employeeAllocation>
</ns3:EmployeeAllocationResponse>
这是“employeeAllocation”类
public class EmployeeAllocation
{
[XmlElement("employee")]
public Employee Employee { get; set; }
[XmlElement("allocatables")]
public List<Alloctable> Alloctables { get; set; }
}
这是“alloctables”类
[XmlType(Namespace = "http://employeeallocation.intf.mb.ivu.de/", TypeName = "Duty")]
public class Alloctable
{
[XmlElement("startTime", Namespace = "")]
public string StartTime { get; set; }
}
我的问题是我不知道如何将一种新的“alloctables”类型添加到我的 Alloctables 列表中。现在它可以处理 type="Duty" 但在尝试反序列化其他类型时崩溃了。
【问题讨论】:
-
反序列化有什么问题,你使用什么代码?
-
抱歉,会更新问题。
标签: c# xml serialization