Xpath and XmlNameSpace

I have the following XML

<?xml version="1.0"?> <FileHeader xmlns="urn:schemas-ncr-com:ECPIX:CXF:FileStructure:020001" VersionNumber="020001" TestFileIndicator="P" CreationDate="13012009" CreationTime="172852" FileID="0000000001" > <Item ItemSeqNo="09011340010009" PayorBankRoutNo="00704524" Amount="398000" AccountNo="000003850010205" SerialNo="000512" TransCode="03" PresentingBankRoutNo="00400019" PresentmentDate="13012009" CycleNo="01" NumOfImageViews="2" ClearingType="01" DocType="D" CurrencyInd="LYD" IQAIgnoreInd="0" CashValueInd="1" TruncatingRTNo="00405117" SpecialHandling="00" RepresentmentCnt="0" MICRRepairFlags="000000" > <AddendA BOFDRoutNo="00400019" BOFDBusDate="13012009" DepositorAcct="0000534983" /> <ImageViewDetail ... 

And I need to reach the "ImageViewDetail" element using the Select (xpath_expression) method of the XmlDocument.NET class.

The following code does not work

 xmlDocument.Select("//Item/AddendA/ImageViewDetail"); 

if i don't delete

 xmlns="urn:schemas-ncr-com:ECPIX:CXF:FileStructure:020001" 

from the fileheader tag

What is the correct way to work with namespace here?

Thanks,

+4
source share
2 answers

With an XmlNamespaceManager and an alias in xpath:

  XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable); mgr.AddNamespace("x", doc.DocumentElement.NamespaceURI); foreach (XmlNode node in doc.SelectNodes( "//x:Item/x:AddendA/x:ImageViewDetail", mgr)) { Console.WriteLine(node.OuterXml); } 
+5
source

Check out http://www.vijaymukhi.com/documents/books/csclasses/chap7.htm , you may find something that might help.

0
source

All Articles