本文介绍了阅读与几个命名空间的XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有几个命名空间的XML文件。但我不能从任何一个节点获得价值/文
I have an XML file with several namespaces. But i cant get value/ text from any of the nodes.
<?xml version="1.0" encoding="UTF-8"?>
<!--XML file created by Microsoft Dynamics C5-->
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
xmlns:ccts="urn:un:unece:uncefact:documentation:2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2">
<cbc:UBLVersionID schemeAgencyID="320" schemeAgencyName="urn:oioubl:id:profileid-1.1">2.0</cbc:UBLVersionID>
<cbc:CustomizationID>OIOUBL-2.02</cbc:CustomizationID>
<cbc:ProfileID schemeID="urn:oioubl:id:profileid-1.2" schemeAgencyID="320">Procurement-BilSim-1.0</cbc:ProfileID>
<cbc:ID>88481</cbc:ID>
<cbc:IssueDate>2012-05-21</cbc:IssueDate>
<cbc:InvoiceTypeCode listID="urn:oioubl:codelist:invoicetypecode-1.1" listAgencyID="320">380</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>DKK</cbc:DocumentCurrencyCode>
<cbc:AccountingCost></cbc:AccountingCost>
<cac:OrderReference>
<cbc:ID>ZZ</cbc:ID>
<cbc:SalesOrderID>36433</cbc:SalesOrderID>
<cbc:IssueDate>2012-05-21</cbc:IssueDate>
</cac:OrderReference>
</Invoice>
我想读一些节点,但我不能。这里有一个例子
I'm trying to read some of the nodes but i cant. Here is an example.
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\temp\88481.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(doc.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");
manager.AddNamespace("", "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
XmlNodeList list = doc.SelectNodes("//Invoice/cbc:ID", manager);
但节点列表中没有的元素?
But the node list have no elements ?
推荐答案
您真的关闭的第一次。
You were really close the first time.
像乔恩斯基特提到的,发票是根元素,所以你并不需要声明一个新的命名空间。
Like Jon Skeet mentioned, "Invoice" is the root element so you don't need to declare a new namespace.
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\temp\88481.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(doc.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");
//manager.AddNamespace("", "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
XmlNodeList list = doc.SelectNodes("//cbc:ID", manager);
这篇关于阅读与几个命名空间的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!