首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UBL2.1XML反序列化C#列表/数组

UBL2.1XML反序列化C#列表/数组
EN

Stack Overflow用户
提问于 2022-10-27 21:18:34
回答 2查看 144关注 0票数 0

我很难对部分发票XML进行反序列化。

标签cac:InvoiceLine正在重复。

我需要帮助为元素InvoiceLine创建列表/数组,并执行foreach循环并从子元素中提取值。

这是XML文件的示例。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<env:DocumentEnvelope xmlns:env="urn:eFaktura:MinFinrs:envelop:schema">
  <env:DocumentHeader>
    <env:SalesInvoiceId>4372797</env:SalesInvoiceId>
    <env:PurchaseInvoiceId>3935145</env:PurchaseInvoiceId>
    <env:DocumentId>3ff1e4d7-9025-4908-b05b-26094758bd7d</env:DocumentId>
    <env:CreationDate>2022-10-06</env:CreationDate>
    <env:SendingDate>2022-10-06</env:SendingDate>
    <env:DocumentPdf mimeCode="application/pdf"></env:DocumentPdf>
  </env:DocumentHeader>
  <env:DocumentBody>
    <Invoice xmlns:cec="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
    xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
    xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:sbt="http://mfin.gov.rs/srbdt/srbdtext" 
    xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
      <cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:mfin.gov.rs:srbdt:2022#conformant#urn:mfin.gov.rs:srbdtext:2022</cbc:CustomizationID>
      <cbc:ID>24</cbc:ID>
      <cbc:IssueDate>2022-10-06</cbc:IssueDate>
      <cbc:DueDate>2022-10-20</cbc:DueDate>
      <cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
      <cbc:Note>PROD</cbc:Note>
      <cbc:DocumentCurrencyCode>RSD</cbc:DocumentCurrencyCode>
      <cac:InvoicePeriod>
        <cbc:StartDate>2022-09-01</cbc:StartDate>
        <cbc:EndDate>2022-09-30</cbc:EndDate>
        <cbc:DescriptionCode>35</cbc:DescriptionCode>
      </cac:InvoicePeriod>
      <cac:AdditionalDocumentReference>
        <cbc:ID>24</cbc:ID>
        <cbc:DocumentType>PRILOG 1</cbc:DocumentType>
        <cac:Attachment>
          <cbc:EmbeddedDocumentBinaryObject mimeCode="application/pdf" encodingCode="base64" filename="24.pdf"></cbc:EmbeddedDocumentBinaryObject>
        </cac:Attachment>
      </cac:AdditionalDocumentReference>
      <cac:AccountingSupplierParty>
        <cac:Party>
          <cbc:EndpointID schemeID="9948">123456789</cbc:EndpointID>
          <cac:PartyIdentification>
            <cbc:ID>81906</cbc:ID>
          </cac:PartyIdentification>
          <cac:PartyName>
            <cbc:Name>SUPPLIER NAME</cbc:Name>
          </cac:PartyName>
          <cac:PostalAddress>
            <cbc:StreetName>SUPPLIER STREET</cbc:StreetName>
            <cbc:CityName>SUPPLIER TOWN</cbc:CityName>
            <cbc:PostalZone>SUPPLIER ZIP CODE</cbc:PostalZone>
            <cac:AddressLine>
              <cbc:Line>STREET,TOWN, ZIP</cbc:Line>
            </cac:AddressLine>
            <cac:Country>
              <cbc:IdentificationCode>RS</cbc:IdentificationCode>
            </cac:Country>
          </cac:PostalAddress>
          <cac:PartyTaxScheme>
            <cbc:CompanyID>RS123456789</cbc:CompanyID>
            <cac:TaxScheme>
              <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
          </cac:PartyTaxScheme>
          <cac:PartyLegalEntity>
            <cbc:RegistrationName>SUPPLIER NAME</cbc:RegistrationName>
            <cbc:CompanyID>98765432</cbc:CompanyID>
          </cac:PartyLegalEntity>
          <cac:Contact>
            <cbc:ElectronicMail>xxx@xxx.rs</cbc:ElectronicMail>
          </cac:Contact>
        </cac:Party>
      </cac:AccountingSupplierParty>
      <cac:AccountingCustomerParty>
        <cac:Party>
          <cbc:EndpointID schemeID="9948">111111111</cbc:EndpointID>
          <cac:PartyIdentification />
          <cac:PartyName>
            <cbc:Name>CUSTOMER NAME</cbc:Name>
          </cac:PartyName>
          <cac:PostalAddress>
            <cbc:StreetName>CUSTOMER street</cbc:StreetName>
            <cbc:CityName>CUSTOMER town</cbc:CityName>
            <cbc:PostalZone>CUSTOMER zip</cbc:PostalZone>
            <cac:AddressLine>
              <cbc:Line>street, town, zip</cbc:Line>
            </cac:AddressLine>
            <cac:Country>
              <cbc:IdentificationCode>RS</cbc:IdentificationCode>
            </cac:Country>
          </cac:PostalAddress>
          <cac:PartyTaxScheme>
            <cbc:CompanyID>RS111111111</cbc:CompanyID>
            <cac:TaxScheme>
              <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
          </cac:PartyTaxScheme>
          <cac:PartyLegalEntity>
            <cbc:RegistrationName>CUSTOMER NAME</cbc:RegistrationName>
            <cbc:CompanyID>22222222</cbc:CompanyID>
          </cac:PartyLegalEntity>
          <cac:Contact>
            <cbc:ElectronicMail>yyy@yyy.rs</cbc:ElectronicMail>
          </cac:Contact>
        </cac:Party>
      </cac:AccountingCustomerParty>
      <cac:Delivery>
        <cbc:ActualDeliveryDate>2022-09-30</cbc:ActualDeliveryDate>
      </cac:Delivery>
      <cac:PaymentMeans>
        <cbc:PaymentMeansCode>30</cbc:PaymentMeansCode>
        <cbc:PaymentID>223985-2209</cbc:PaymentID>
        <cac:PayeeFinancialAccount>
          <cbc:ID>100-000000-11</cbc:ID>
        </cac:PayeeFinancialAccount>
      </cac:PaymentMeans>
      <cac:TaxTotal>
        <cbc:TaxAmount currencyID="RSD">967.2</cbc:TaxAmount>
        <cac:TaxSubtotal>
          <cbc:TaxableAmount currencyID="RSD">9464</cbc:TaxableAmount>
          <cbc:TaxAmount currencyID="RSD">946.4</cbc:TaxAmount>
          <cac:TaxCategory>
            <cbc:ID>S</cbc:ID>
            <cbc:Percent>10</cbc:Percent>
            <cac:TaxScheme>
              <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
          </cac:TaxCategory>
        </cac:TaxSubtotal>
        <cac:TaxSubtotal>
          <cbc:TaxableAmount currencyID="RSD">104</cbc:TaxableAmount>
          <cbc:TaxAmount currencyID="RSD">20.8</cbc:TaxAmount>
          <cac:TaxCategory>
            <cbc:ID>S</cbc:ID>
            <cbc:Percent>20</cbc:Percent>
            <cac:TaxScheme>
              <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
          </cac:TaxCategory>
        </cac:TaxSubtotal>
      </cac:TaxTotal>
      <cac:LegalMonetaryTotal>
        <cbc:LineExtensionAmount currencyID="RSD">9568</cbc:LineExtensionAmount>
        <cbc:TaxExclusiveAmount currencyID="RSD">9568</cbc:TaxExclusiveAmount>
        <cbc:TaxInclusiveAmount currencyID="RSD">10535.2</cbc:TaxInclusiveAmount>
        <cbc:AllowanceTotalAmount currencyID="RSD">0</cbc:AllowanceTotalAmount>
        <cbc:PrepaidAmount currencyID="RSD">0</cbc:PrepaidAmount>
        <cbc:PayableAmount currencyID="RSD">10535.2</cbc:PayableAmount>
      </cac:LegalMonetaryTotal>
      <cac:InvoiceLine>
        <cbc:ID>1</cbc:ID>
        <cbc:InvoicedQuantity unitCode="MTQ">65</cbc:InvoicedQuantity>
        <cbc:LineExtensionAmount currencyID="RSD">3971.5</cbc:LineExtensionAmount>
        <cac:Item>
          <cbc:Name>line 1 description</cbc:Name>
          <cac:SellersItemIdentification>
            <cbc:ID>1</cbc:ID>
          </cac:SellersItemIdentification>
          <cac:ClassifiedTaxCategory>
            <cbc:ID>S</cbc:ID>
            <cbc:Percent>10</cbc:Percent>
            <cac:TaxScheme>
              <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
          </cac:ClassifiedTaxCategory>
        </cac:Item>
        <cac:Price>
          <cbc:PriceAmount currencyID="RSD">61.1</cbc:PriceAmount>
          <cbc:BaseQuantity unitCode="MTQ">1</cbc:BaseQuantity>
        </cac:Price>
      </cac:InvoiceLine>
      <cac:InvoiceLine>
        <cbc:ID>2</cbc:ID>
        <cbc:InvoicedQuantity unitCode="MTQ">65</cbc:InvoicedQuantity>
        <cbc:LineExtensionAmount currencyID="RSD">2535</cbc:LineExtensionAmount>
        <cac:Item>
          <cbc:Name>line 2 description</cbc:Name>
          <cac:SellersItemIdentification>
            <cbc:ID>3</cbc:ID>
          </cac:SellersItemIdentification>
          <cac:ClassifiedTaxCategory>
            <cbc:ID>S</cbc:ID>
            <cbc:Percent>10</cbc:Percent>
            <cac:TaxScheme>
              <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
          </cac:ClassifiedTaxCategory>
        </cac:Item>
        <cac:Price>
          <cbc:PriceAmount currencyID="RSD">39</cbc:PriceAmount>
          <cbc:BaseQuantity unitCode="MTQ">1</cbc:BaseQuantity>
        </cac:Price>
      </cac:InvoiceLine>
    </Invoice>
  </env:DocumentBody>
</env:DocumentEnvelope>

我在Visual中生成了类

将XML数据复制到剪贴板

2在VS中,编辑>粘贴特殊>“粘贴Xml作为类”

这是我从xml获取所有其他值的代码。

代码语言:javascript
复制
static void Main(string[] args)
        {
            XmlSerializer serializer =
            new XmlSerializer(typeof(XmlStr.DocumentEnvelope));
            // Declare an object variable of the type to be deserialized.
            XmlStr.DocumentEnvelope envelope;
            using (Stream reader = new FileStream(@"C:\\Users\\Desktop\\24.xml", FileMode.Open))
            {
                // Call the Deserialize method to restore the object's state.
                envelope = (XmlStr.DocumentEnvelope)serializer.Deserialize(reader);
            }
            // Write out the properties of the object.
            Console.WriteLine(envelope.DocumentBody.Invoice.ID.Value);
        }

当我使用下面的代码时,会出现错误。

System.InvalidOperationException:“XML文档(2,2)中有一个错误。”

内部异常

InvalidOperationException:没想到<DocumentEnvelope xmlns='urn:eFaktura:MinFinrs:envelop:schema'>

代码语言:javascript
复制
XmlSerializer serializer =
           new XmlSerializer(typeof(zaglavlje.InvoiceLine[]));
            // Declare an object variable of the type to be deserialized.
            zaglavlje.InvoiceLine[] envelope;
            using (Stream reader = new FileStream(@"C:\\Users\\Desktop\\24.xml", FileMode.Open))
            {
                // Call the Deserialize method to restore the object's state.
                envelope = (zaglavlje.InvoiceLine[])serializer.Deserialize(reader);
            }
            // Write out the properties of the object.
            //Console.WriteLine(envelope);

我想要这样的InvoiceLine

第一环

ID: 1

InvoicedQuantity: 65

LineExtensionAmount: 3971.5

Item.Name:第1行描述

Price.PriceAmount: 61.1

BaseQuantity: 1

第二回路

ID: 2

InvoicedQuantity: 65

LineExtensionAmount: 2535

Item.Name:第2行描述

Price.PriceAmount: 39

BaseQuantity: 1

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-28 00:28:29

使用客户序列化:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(DocumentEnvelope));
            DocumentEnvelope envelope = (DocumentEnvelope) serializer.Deserialize(reader);
        }
    }
    [XmlRoot(Namespace = "urn:eFaktura:MinFinrs:envelop:schema")]
    public class DocumentEnvelope
    {
        public DocumentBody DocumentBody { get; set; }
    }
    public class DocumentBody
    {
        [XmlElement(Namespace = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2")]
        public Invoice Invoice { get; set; }
    }
    public class Invoice
    {
        [XmlElement(ElementName = "InvoiceLine", Namespace = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")]
        public List<InvoiceLine> InvoiceLine { get; set; }
    }
    public class InvoiceLine : IXmlSerializable
    {
        public int InvoicedQuantity { get; set; }
        public decimal LineExtensionAmount { get; set; }
        public string ItemName { get; set; }
        public decimal PriceAmount { get; set; }
        public decimal  BaseQuantity { get; set; }

        public void WriteXml(XmlWriter writer)
        {
            
        }

        public void ReadXml(XmlReader reader)
        {
            XElement invoiceLine = (XElement)XElement.ReadFrom(reader);
            XNamespace ns = invoiceLine.Name.Namespace;


            InvoicedQuantity = (int)invoiceLine.Elements().Where(x => x.Name.LocalName == "InvoicedQuantity").First();
            LineExtensionAmount = (decimal)invoiceLine.Elements().Where(x => x.Name.LocalName == "LineExtensionAmount").First();
            ItemName = (string)invoiceLine.Descendants().Where(x => x.Name.LocalName == "Name").FirstOrDefault();
            PriceAmount = (decimal)invoiceLine.Descendants().Where(x => x.Name.LocalName == "PriceAmount").FirstOrDefault();
            BaseQuantity = (decimal )invoiceLine.Descendants().Where(x => x.Name.LocalName == "BaseQuantity").FirstOrDefault();
        }
        public XmlSchema GetSchema()
        {
            return (null);
        }

    }


}
票数 1
EN

Stack Overflow用户

发布于 2022-10-28 10:21:35

谢谢@jdweng

我试过你的代码,效果很好。

我还成功地修改了我的代码,以获得InvoiceLine的计数和do循环。

代码语言:javascript
复制
static void Main(string[] args)
{

    XmlSerializer serializer =
    new XmlSerializer(typeof(zaglavlje.DocumentEnvelope));
    // Declare an object variable of the type to be deserialized.
    zaglavlje.DocumentEnvelope envelope;
    using (Stream reader = new FileStream(FILENAME, FileMode.Open))
    {
        // Call the Deserialize method to restore the object's state.
        envelope = (zaglavlje.DocumentEnvelope)serializer.Deserialize(reader);
    }

    int xxx = envelope.DocumentBody.Invoice.InvoiceLine.Count();
    for (int i = 0; i < xxx; i++)
    {
        Console.WriteLine("No: " + envelope.DocumentBody.Invoice.InvoiceLine[i].ID.Value);
        Console.WriteLine("Name: " + envelope.DocumentBody.Invoice.InvoiceLine[i].Item.Name);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74228434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档