views:

73

answers:

2

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 element "ImageViewDetail" using Select(xpath_expression) method of XmlDocument .NET class.

The following code is not working

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

unless I remove

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

from "FileHeader" tag

What is the correct way to deal with namespace here?

Thank you,

A: 

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

abmv
+2  A: 

With an XmlNamespaceManager and an alias in the 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);
    }
Marc Gravell