Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML: Colon in my element causes XPath to miss it

I have an XML document that I load in and try to search with XPath. The root node in this file is <t:Transmission xmlns:t='urn:InboundShipment'> and the file end is properly closed with </t:Transmission>.

My problem is that I cannot walk the tree without using a descendant axis. In other words, I can do: SelectSingleNode("//TransactionHeader[SHIPPERSTATE='CA']") and get a node in return. But I cannot do what should be the equivalent: SelectSingleNode("/Transmission/TransmissionBody/Transaction/TransactionHeader[SHIPPERSTATE='CA']")

If I remove the t: I can do an XPath search on /Transmission and get the whole file. With the t: in there I just get null. Or if I try SelectSingleNode("t:Transmission") I get an error with my XPath statement.

I generally do not need to query the root element, so I should be able to make do with just using the descendant axis for my searches. But the XML looks valid to me and so I'd like to know how to address this. Plus I don't want to ask the client to remove "t:" just because I don't know how to deal with it.

like image 930
Mike K Avatar asked Oct 16 '09 17:10

Mike K


People also ask

What is colon in XML tag?

The colons indicate that those elements are in the ns1 namespace. This is needed when you are using multiple schemas. Assuming the document only uses ns1, it is essentially equivalent to not having any "ns1:" in those tags.

Does XPath require namespace?

XPath queries are aware of namespaces in an XML document and can use namespace prefixes to qualify element and attribute names. Qualifying element and attribute names with a namespace prefix limits the nodes returned by an XPath query to only those nodes that belong to a specific namespace.

Is XPath the same as XML?

XPath grew out of efforts to share a common syntax between XSL (XSLT) and XPointer transformations. It allows the search and retrieval of information within an XML document structure. XPath is not an XML syntax: rather, it uses a syntax that refers to the logical structure of an XML document.


1 Answers

The "t:" is a namespace prefix, which is bound to the namespace 'urn:InboundShipment.' In order to properly handle it, you have to tell c# what the prefix is bound to. This page should explain how to use System.Xml.XmlNamespaceManager to handle the namespace.

Edit: See this answer, as well.

like image 111
James Sulak Avatar answered Oct 21 '22 12:10

James Sulak