我目前正在尝试在C#中反序列化以下XML文件。
问题是应该包含订单位置的第二个列表TraceLinkOrder
没有正确地反序列化。
<?xml version="1.0" encoding="UTF-8"?>
<tli:AddTracelinkOrders xmlns:tli="urn:microsoft-dynamics-schemas/codeunit/CarismaIntegration" xmlns:x50="urn:microsoft-dynamics-nav/xmlports/x50020">
<tli:carismaOrders>
<x50:TracelinkOrder DocType="0" Annulation="false" TROrderType="1" Department="08">
<x50:TracelinkOrderLine NetValue="42.00" AccountNo="100206" LineNo="10000"/>
<x50:TracelinkOrderLine NetValue="259.17" AccountNo="100306" LineNo="20000"/>
<x50:TracelinkOrderLine NetValue="385.80" AccountNo="100406" LineNo="30000"/>
</x50:TracelinkOrder>
<x50:TracelinkOrder DocType="1" Annulation="false" TROrderType="1" Department="08">
<x50:TracelinkOrderLine NetValue="2.10" AccountNo="100206" LineNo="10000"/>
<x50:TracelinkOrderLine NetValue="25.92" AccountNo="100306" LineNo="20000"/>
</x50:TracelinkOrder>
</tli:carismaOrders>
</tli:AddTracelinkOrders>
家长班
[XmlRoot("AddTracelinkOrders", Namespace = "urn:microsoft-dynamics-schemas/codeunit/CarismaIntegration")]
public class TraceLinkOrders
{
[XmlArray("carismaOrders", Namespace = "urn:microsoft-dynamics-schemas/codeunit/CarismaIntegration")]
[XmlArrayItem("TracelinkOrder", Namespace = "urn:microsoft-dynamics-nav/xmlports/x50020")]
public List<TraceLinkOrder> CarismaOrders { get; set; }
}
订购
[XmlRoot("TracelinkOrder")]
public class TraceLinkOrder
{
[XmlAttribute()]
public Int16 DocType { get; set; }
[XmlAttribute()]
public Boolean Annulation { get; set; }
[XmlAttribute()]
public Int16 TROrderType { get; set; }
[XmlAttribute()]
public Int16 Department { get; set; }
}
订单行
[XmlRoot("TracelinkOrderLine", Namespace = "urn:microsoft-dynamics-nav/xmlports/x50020")]
public class TraceLinkOrderLine
{
[XmlAttribute()]
public Decimal NetValue { get; set; }
[XmlAttribute()]
public String AccountNo { get; set; }
[XmlAttribute()]
public String LineNo { get; set; }
}
订单类应包含订单行列表。
我试图像这样向订单类添加属性:
[XmlArrayItem("TracelinkOrderLine", Namespace = "urn:microsoft-dynamics-nav/xmlports/x50020")]
public List<TraceLinkOrderLine> OrderLines { get; set; }
不幸的是,该列表始终为空。
我的另一个想法是使TraceLinkOrder
扩展List<TraceLinkOrderLine>
,但这也不起作用。
[XmlRoot("TracelinkOrder")]
public class TraceLinkOrder : List<TraceLinkOrderLine>
当我这样做时,CarismaOrders
列表为空。
如何在仍然能够访问元素TraceLinkOrder
和TraceLinkOrderLine
的所有属性的同时反序列化此XML文件?
答案 0 :(得分:0)
正如jdweng在我的问题的评论中指出的,窍门是使用XmlElement
属性而不是XmlArrayItem
。
这将告诉C#接受该名称的所有元素,并与它们形成一个列表。
TraceLinkOrder
类的最终代码如下:
[XmlRoot("TracelinkOrder")]
public class TraceLinkOrder
{
[XmlAttribute()]
public Int16 DocType { get; set; }
[XmlAttribute()]
public Boolean Annulation { get; set; }
[XmlAttribute()]
public Int16 TROrderType { get; set; }
[XmlAttribute()]
public Int16 Department { get; set; }
[XmlElement("TracelinkOrderLine")]
public List<TraceLinkOrderLine> OrderLines { get; set; }
}