我正在尝试读取包含电子发票的 xml 文件的内容。为此,我使用命名空间,但无法提取 cac:AdditionalItemProperty 元素中找到的数据,特别是 cbc:Name02cbc:Value2023171843 我所做的代码如下,并且是 xml 的另外一部分。谢谢你的帮助
文件 XML
<ext:UBLExtensions>
...
</ext:UBLExtensions>
<cbc:UBLVersionID>UBL 2.1</cbc:UBLVersionID>
<cbc:CustomizationID>Doctos</cbc:CustomizationID>
<cbc:ID>DT41647</cbc:ID>
<cbc:IssueDate>2023-03-17</cbc:IssueDate>
<cbc:ParentDocumentID>DT41647</cbc:ParentDocumentID>
<cac:SenderParty>
...
</cac:SenderParty>
<cac:ReceiverParty>
...
</cac:ReceiverParty>
<cac:Attachment>
<cac:ExternalReference>
<cbc:MimeCode>text/xml</cbc:MimeCode>
<cbc:EncodingCode>UTF-8</cbc:EncodingCode>
<cbc:Description>
<![CDATA[ <?xml version="1.0" encoding="UTF-8" standalone="no"?><Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sts="xxxx" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:oth="http://example.org/oth" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"><ext:UBLExtensions><ext:UBLExtension><ext:ExtensionContent>
<sts:xxxExtensions><sts:InvoiceControl><sts:InvoiceAuthorization>99999999</sts:InvoiceAuthorization><sts:AuthorizationPeriod><cbc:StartDate>2023-02-23</cbc:StartDate><cbc:EndDate>2024-02-23</cbc:EndDate></sts:AuthorizationPeriod><cac:AdditionalItemProperty><cbc:Name>01</cbc:Name><cbc:Value>105584152</cbc:Value></cac:AdditionalItemProperty><cac:AdditionalItemProperty><cbc:Name>02</cbc:Name><cbc:Value>2023171843</cbc:Value></cac:AdditionalItemProperty><cac:AdditionalItemProperty><cbc:Name>03</cbc:Name><cbc:Value>184000</cbc:Value><cbc:ValueQuantity unitCode="KG">1280</cbc:ValueQuantity></cac:AdditionalItemProperty></cac:Item><cac:Price><cbc:PriceAmount currencyID="PP">184000.00</cbc:PriceAmount><cbc:BaseQuantity unitCode="94">1.00</cbc:BaseQuantity></cac:Price></cac:InvoiceLine></Invoice> ]]>
</cbc:Description>
</cac:ExternalReference>
</cac:Attachment>
<cac:ParentDocumentLineReference>
</cac:ParentDocumentLineReference>
</AttachedDocument>
'代码VbNet
m_xmld.Load(fileName)
Dim manager As XmlNamespaceManager = New XmlNamespaceManager(m_xmld.NameTable)
manager.AddNamespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")
manager.AddNamespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")
'manager.AddNamespace("qdt", "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2")
'manager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
'manager.AddNamespace("udt", "urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2")
'manager.AddNamespace("ccts", "urn:un:unece:uncefact:documentation:2")
manager.AddNamespace("ext", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2")
Dim list As XmlNodeList = m_xmld.SelectNodes("//cac:Attachment//cac:ExternalReference//cbc:Description", manager) 'da resultado 2
'Dim nodeComprobante As XmlNode = m_xmld.SelectSingleNode("//cac:AdditionalItemProperty", manager)
Dim nodeComprobante As XmlNode
For Each Book As XmlNode In list
nodeComprobante = m_xmld.SelectSingleNode("//cac:AdditionalItemProperty", manager)
'TotalXML += Book.SelectSingleNode("//cac:AdditionalItemProperty", manager).InnerXml 'error
'Dim mCodigo As Object = New Book.Attributes.GetNamedItem("Name").Value 'error
Next
MessageBox.Show("Tota: " & TotalXML, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
您在 CDATA 部分中有第二个 XML 文件。您需要单独解析它。就像下面的代码一样。我无法测试它,因为您包含的 XML 似乎不完整:
Dim m_xmld = New XmlDocument
Dim manager As XmlNamespaceManager = New XmlNamespaceManager(m_xmld.NameTable)
manager.AddNamespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")
manager.AddNamespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")
manager.AddNamespace("ext", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2")
m_xmld.Load(fileName)
Dim nodeDescription As XmlNode = m_xmld.SelectSingleNode("//cbc:Description", manager)
Dim childDoc = New XmlDocument
childDoc.Load(nodeDescription.FirstChild.InnerText) '<< parse the CDATA section
Dim additional = childDoc.SelectSingleNode("//cac:AdditionalItemProperty[0]")
MessageBox.Show(additional.InnerText, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)