C#:逐行解析XML

时间:2018-06-08 08:30:02

标签: c# xml

我正在尝试解析XML Document并将其转换为C#对象。我成功了几个条目,但对于其他人我不明白为什么它不起作用。这是示例xml:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2">
    <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
    <cbc:CustomizationID>abcd</cbc:CustomizationID>
    <cbc:ProfileID>urn:www.abc.eu:profile:bii05:ver2.0</cbc:ProfileID>
    <cbc:ID>TOSL108</cbc:ID>
    <cbc:IssueDate>2013-06-30</cbc:IssueDate>
    <cbc:InvoiceTypeCode listID="UL122001">38021</cbc:InvoiceTypeCode>
    <cbc:Note>Ordered in our booth at the convention.</cbc:Note>
    <cbc:TaxPointDate>2013-06-30</cbc:TaxPointDate>
    <cbc:DocumentCurrencyCode listID="ISO4217">NOK</cbc:DocumentCurrencyCode>
    <cbc:AccountingCost>Project cost code 123</cbc:AccountingCost>
    <cac:InvoicePeriod>
        <cbc:StartDate>2013-06-01</cbc:StartDate>
        <cbc:EndDate>2013-06-30</cbc:EndDate>
    </cac:InvoicePeriod>
</Invoice>  

我成功获得了所有的cbc项目但没有获得cac。这是c#代码:

XmlDocument document = new XmlDocument();
document.LoadXml(model.XmlDocument);

XmlNamespaceManager mgr = new XmlNamespaceManager(document.NameTable);
mgr.AddNamespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2");
mgr.AddNamespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2");
XmlElement docElement = document.DocumentElement;
var a = docElement.SelectSingleNode("cbc:InvoiceTypeCode/@listID", mgr)?.InnerText;
var b = docElement.SelectSingleNode("cbc:TaxCurrencyCode/@listID", mgr)?.InnerText;
var invoice = new Invoice()
{
    UBLVersionId = docElement.SelectSingleNode("cbc:UBLVersionID", mgr)?.InnerText,
    CustomizationID = docElement.SelectSingleNode("cbc:CustomizationID", mgr)?.InnerText,
    ProfileID = docElement.SelectSingleNode("cbc:ProfileID", mgr)?.InnerText,
    InvoiceId = docElement.SelectSingleNode("cbc:InvoiceId", mgr)?.InnerText,
    IssueDate = DateTime.Parse(docElement.SelectSingleNode("cbc:IssueDate", mgr)?.InnerText),
    InvoiceTypeCode = docElement.SelectSingleNode("cbc:InvoiceTypeCode", mgr)?.InnerText,
    InvoiceTypeCode_ListID = docElement.SelectSingleNode("cbc:InvoiceTypeCode/@listID", mgr)?.InnerText,
    TaxPointDate = docElement.SelectSingleNode("cbc:TaxPointDate", mgr)?.InnerText,
    DocumentCurrencyCode = docElement.SelectSingleNode("cbc:DocumentCurrencyCode", mgr)?.InnerText,
    DocumentCurrencyCodeListID = docElement.SelectSingleNode("cbc:DocumentCurrencyCode/@listID", mgr)?.InnerText,
    TaxCurrencyCode = docElement.SelectSingleNode("cbc:TaxCurrencyCode", mgr)?.InnerText,
    TaxCurrencyCodeListID = docElement.SelectSingleNode("cbc:TaxCurrencyCode/@listID", mgr)?.InnerText,
    AccountingCost = docElement.SelectSingleNode("cbc:AccountingCost", mgr)?.InnerText,
    InvoicePeriod = new InvoicePeriod()
    {
        StartDate = DateTime.Parse(docElement.SelectSingleNode("cac:InvoicePeriod/cbc:StartDate", mgr)?.InnerText),
        EndDate = DateTime.Parse(docElement.SelectSingleNode("cac:InvoicePeriod/cbc:EndDate", mgr)?.InnerText)
    }
};  
  1. 为什么InvoicePeriod解析不起作用?
  2. 是否有更有效的方法来解析这样的文档?

3 个答案:

答案 0 :(得分:2)

使用c = [] Lon3 = [] Lat3 = [] cnt = 0 for lo1,la1 in zip(Lon1, Lat1): for lo2,la2 in zip(Lon2, Lat2): if (lo1 == lo2 and la1 == la2) { c.append(a[cnt]) Lon3.append(lo1) Lat3.append(la1) break } cnt++ 让您的生活更轻松。通过使用这个类,您将获得一个可维护的代码。所以你会有类似下面的内容:

System.Xml.Serialization.XmlSerializer

为避免像您在评论中所说的那样执行Invoice invoice; XmlSerializer serializer = new XmlSerializer(typeof(Invoice)); using (var reader = new StringReader(model.XmlDocument)) { invoice = (Invoice)serializer.Deserialize(reader); } ,您需要在Visual Studio中执行此操作编辑&gt; 选择性粘贴&gt; 粘贴为XML类。因此,VS将为您生成发票的正确类结构。

答案 1 :(得分:2)

我使用Xml2CSharp工具创建了以下课程。

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
    [XmlRoot(ElementName="InvoiceTypeCode", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
    public class InvoiceTypeCode {
        [XmlAttribute(AttributeName="listID")]
        public string ListID { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName="DocumentCurrencyCode", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
    public class DocumentCurrencyCode {
        [XmlAttribute(AttributeName="listID")]
        public string ListID { get; set; }
        [XmlText]
        public string Text { get; set; }
    }

    [XmlRoot(ElementName="InvoicePeriod", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")]
    public class InvoicePeriod {
        [XmlElement(ElementName="StartDate", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public string StartDate { get; set; }
        [XmlElement(ElementName="EndDate", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public string EndDate { get; set; }
    }

    [XmlRoot(ElementName="Invoice", Namespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2")]
    public class Invoice {
        [XmlElement(ElementName="UBLVersionID", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public string UBLVersionID { get; set; }
        [XmlElement(ElementName="CustomizationID", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public string CustomizationID { get; set; }
        [XmlElement(ElementName="ProfileID", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public string ProfileID { get; set; }
        [XmlElement(ElementName="ID", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public string ID { get; set; }
        [XmlElement(ElementName="IssueDate", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public string IssueDate { get; set; }
        [XmlElement(ElementName="InvoiceTypeCode", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public InvoiceTypeCode InvoiceTypeCode { get; set; }
        [XmlElement(ElementName="Note", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public string Note { get; set; }
        [XmlElement(ElementName="TaxPointDate", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public string TaxPointDate { get; set; }
        [XmlElement(ElementName="DocumentCurrencyCode", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public DocumentCurrencyCode DocumentCurrencyCode { get; set; }
        [XmlElement(ElementName="AccountingCost", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")]
        public string AccountingCost { get; set; }
        [XmlElement(ElementName="InvoicePeriod", Namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")]
        public InvoicePeriod InvoicePeriod { get; set; }
        [XmlAttribute(AttributeName="xmlns")]
        public string Xmlns { get; set; }
        [XmlAttribute(AttributeName="cac", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Cac { get; set; }
        [XmlAttribute(AttributeName="cbc", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Cbc { get; set; }
        [XmlAttribute(AttributeName="ccts", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Ccts { get; set; }
        [XmlAttribute(AttributeName="qdt", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Qdt { get; set; }
        [XmlAttribute(AttributeName="udt", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Udt { get; set; }
    }

}

现在可以使用以下代码反序列化为C#对象。

XmlSerializer serializer = new XmlSerializer(typeof(Invoice));
using (var stringReader = new StringReader(model.XmlDocument))
{
    Invoice obj = (Invoice)serializer.Deserialize(stringReader);
}

答案 2 :(得分:2)

我不知道为什么许多用户不使用VS功能 - 选择性粘贴,但您可以复制xml字符串,然后:

  

编辑 - &gt;选择性粘贴 - &gt;将XML粘贴为CLASES

和VS将为您生成必要的课程。

然后,您可以使用 XMLSerializer class 反序列化您的xml字符串:

XmlSerializer serializer = new XmlSerializer(typeof(Invoice));
Invoice invoice = null;
using (StringReader sr = new StringReader(xmlString))
{
    invoice = serializer.Deserialize(sr) as Invoice;
}

示例:DotNetFiddle Example