如何解析具有相同标签名称的XML

时间:2019-03-22 13:07:54

标签: c# xml

我有这个XML文件,并且能够读取它,但是一旦到达item标签,它就会中断并返回null。注意:我只包含了给我带来问题的XML代码段。

      <ApproversList>
      <item>
         <ApprovalDate>2019-03-12</ApprovalDate>
         <Approved>1</Approved>
         <ApproverComment>ERP posting error (ERP integration timeout): Posted by Service User</ApproverComment>
         <ApproverEmail />
         <ApproverID>filemanager.su@100039144</ApproverID>
         <ApproverLabelRole>File Manager</ApproverLabelRole>
         <Approver>File Manager</Approver>
      </item>
      <item>
         <ApprovalDate />
         <Approved>0</Approved>
         <ApproverComment />
         <ApproverEmail />
         <ApproverID>filemanagersprocess.su@100039144</ApproverID>
         <ApproverLabelRole>File Manager</ApproverLabelRole>
         <Approver>File Manager</Approver>
      </item>
   </ApproversList>
   <LineItems>
      <item>
         <Amount>3746.29</Amount>
         <Assignment />
         <BusinessArea />
         <CCDescription>UTILITIES DEEK WAREHOUSE</CCDescription>
         <CostCenter>66999939393</CostCenter>
         <Description />
         <GLAccount>12345</GLAccount>
         <GLDescription>Jimmy Johns</GLDescription>
         <InternalOrder />
         <LineType>KL</LineType>
         <TaxAmount>0</TaxAmount>
         <TaxCode>(SALES)</TaxCode>
         <TaxJurisdiction />
         <TaxRate>0</TaxRate>
      </item>
   </LineItems>

当前我的课程设置是:

namespace XmlDeserializer
{
    [XmlRoot(ElementName = "ApproversList")]
    public class ApproversList
    {
        [XmlElement(ElementName = "item")]
        public Item Item { get; set; }
    }

    [XmlRoot(ElementName = "item")]
    public class Item
    {
        [XmlElement(ElementName = "ApprovalDate")]
        public string ApprovalDate { get; set; }
        [XmlElement(ElementName = "Approved")]
        public string Approved { get; set; }
        [XmlElement(ElementName = "ApproverComment")]
        public string ApproverComment { get; set; }
        [XmlElement(ElementName = "ApproverEmail")]
        public string ApproverEmail { get; set; }
        [XmlElement(ElementName = "ApproverID")]
        public string ApproverID { get; set; }
        [XmlElement(ElementName = "ApproverLabelRole")]
        public string ApproverLabelRole { get; set; }
        [XmlElement(ElementName = "Approver")]
        public string Approver { get; set; }
        [XmlElement(ElementName = "Amount")]
        public string Amount { get; set; }
        [XmlElement(ElementName = "Assignment")]
        public string Assignment { get; set; }
        [XmlElement(ElementName = "BusinessArea")]
        public string BusinessArea { get; set; }
        [XmlElement(ElementName = "CCDescription")]
        public string CCDescription { get; set; }
        [XmlElement(ElementName = "CostCenter")]
        public string CostCenter { get; set; }
        [XmlElement(ElementName = "DeliveryNote")]
        public string DeliveryNote { get; set; }
        [XmlElement(ElementName = "Description")]
        public string Description { get; set; }
        [XmlElement(ElementName = "GLAccount")]
        public string GLAccount { get; set; }
        [XmlElement(ElementName = "GLDescription")]
        public string GLDescription { get; set; }
        [XmlElement(ElementName = "GoodReceipt")]
        public string GoodReceipt { get; set; }
        [XmlElement(ElementName = "InternalOrder")]
        public string InternalOrder { get; set; }
        [XmlElement(ElementName = "ItemNumber")]
        public string ItemNumber { get; set; }
        [XmlElement(ElementName = "LineType")]
        public string LineType { get; set; }
        [XmlElement(ElementName = "OrderNumber")]
        public string OrderNumber { get; set; }
        [XmlElement(ElementName = "Quantity")]
        public string Quantity { get; set; }
        [XmlElement(ElementName = "TaxAmount")]
        public string TaxAmount { get; set; }
        [XmlElement(ElementName = "TaxCode")]
        public string TaxCode { get; set; }
        [XmlElement(ElementName = "TaxJurisdiction")]
        public string TaxJurisdiction { get; set; }
        [XmlElement(ElementName = "TaxRate")]
        public string TaxRate { get; set; }
    }
}

反序列化XML的代码:

public static void DeserializeToObject(out Invoice invoice)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Invoice));
    invoice = new Invoice();

    using (FileStream fileStream = new FileStream(@"FILEPATH", FileMode.Open))
    {
        Invoice result = (Invoice)serializer.Deserialize(fileStream);
    }
}

我尝试了多种可能的解决方案,但无法使其正常工作。这是我的不得已。

1 个答案:

答案 0 :(得分:6)

items是一个集合。将课程更改为:

[XmlRoot(ElementName = "ApproversList")]
public class ApproversList
{
    [System.Xml.Serialization.XmlElementAttribute("item")]
    public Item[] Item { get; set; }
}

将来的技巧,复制XML,转到Visual Studio,在新的C#文件中,单击“编辑”->“选择性粘贴”->“将XML作为类粘贴”

d