gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:52:07 24 4
gpt4 key购买 nike

我有这个 XML 文件并且能够读取它,但是一旦到达项目标签,它就会中断并返回 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>

目前我的类(class)设置是:

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);
}
}

我已经尝试了多种可能的解决方案,但无法使其正常工作。这是我最后的选择。

最佳答案

items 是一个集合。将您的类(class)更改为:

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

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

d

关于c# - 如何解析具有相同标签名称的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55300327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com