Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read XML file with several namespaces

Tags:

c#

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 ?

like image 203
Jes Gudiksen Avatar asked Oct 23 '25 03:10

Jes Gudiksen


2 Answers

Given the comment, I'm going to assume you could use LINQ to XML instead:

XDocument doc = XDocument.Load("...");
XNamespace cbcNs = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";
XNamespace cacNs = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2";
// etc
XNamespace topNs = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2";

var ids = doc.Elements(topNs + "Invoice")
             .Elements(cbc + "ID");

Or as the top one is just the root element, the final statement can just be:

var ids = doc.Root.Elements(cbc + "ID");
like image 168
Jon Skeet Avatar answered Oct 24 '25 19:10

Jon Skeet


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);
like image 40
Oceans Avatar answered Oct 24 '25 19:10

Oceans