Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Friends,

I am not able to traverse through Xpath, till /x:GetPositionByAccountResponse/x:GetPositionByAccountResult/ its picking up
but i need office number from the current XML

Sample XML and code given below.
Please help

C#
static void Main(string[] args)
        {
            NameTable nt = new NameTable();
            XmlNamespaceManager mgr = new XmlNamespaceManager(nt);
            mgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
            mgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
            mgr.AddNamespace("x", "http://tempuri.org/");
            mgr.AddNamespace("y", "http://ebc.mssb.com/");
            XmlDocument XMLDoc = new XmlDocument();
            XMLDoc.Load("XMLFile1.xml");
            string xpath = "/x:GetPositionByAccountResponse/x:GetPositionByAccountResult/y:Message/y:EBCMessageType[2]/y:Message/y:NewDataSet/y:Positions/y:Office";
            foreach (XmlNode ele1 in XMLDoc.SelectNodes(xpath, mgr))
            {
                Console.WriteLine(ele1.InnerXml.ToString());
            }
        }



XML
<GetPositionByAccountResponse xmlns="http://tempuri.org/">
  <GetPositionByAccountResult>
    <ReferenceID xmlns="http://ebc.mssb.com">PKCUb5XThXoIzsqAa71</ReferenceID>
    <ReceivedTime xmlns="http://ebc.mssb.com">2015-11-09T02:43:41.5535744-05:00</ReceivedTime>
    <CompletedTime xmlns="http://ebc.mssb.com">2015-11-09T02:43:41.9246115-05:00</CompletedTime>
    <StatusCode xmlns="http://ebc.mssb.com">Success</StatusCode>
    <Message xmlns="http://ebc.mssb.com">
      <EBCMessageType>
        <MessageType>COMMAND_STRING</MessageType>
      </EBCMessageType>
      <EBCMessageType>
        <Message>
          <NewDataSet>
            <Positions>
              <Office>101</Office>
              <Account>14716</Account>
              <KeyAccount>1999-08-27-17.24.19.730418</KeyAccount>
              <TradeControl>0</TradeControl>
            </Positions>
          </NewDataSet>
        </Message>
        <MessageType>RAW_DATA</MessageType>
      </EBCMessageType>
    </Message>
  </GetPositionByAccountResult>
</GetPositionByAccountResponse>
Posted
Comments
Leo Chapiro 9-Nov-15 7:14am    
Change xpath to string xpath = "//Office";
You can use SelectSingleNode-Methode as well: in this case no loop is necessary
jinesh sam 9-Nov-15 7:39am    
But even it dosen't reach till office. can u please reply with exact path.
in real time i need to use loop

1 solution

Well, this one works:
C#
static void Main(string[] args)
{
   var xmlDoc = new XmlDocument();
   XMLDoc.Load("XMLFile1.xml");

   XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
   manager.AddNamespace("ns", "http://ebc.mssb.com");

   XmlNode root = xmlDoc.DocumentElement;

   const string xpath = "//ns:Office";

   if (root != null)
   {
        var xmlNodeList = root.SelectNodes(xpath, manager);
        if (xmlNodeList == null) return;

        foreach (XmlNode ele1 in xmlNodeList)
           Console.WriteLine(ele1.InnerXml);
    }
}
 
Share this answer
 
v3
Comments
jinesh sam 9-Nov-15 8:33am    
Thanks Dude :) Need to google about "//"
Leo Chapiro 9-Nov-15 8:35am    
You are welcome!
"//" defines a path to node anywhere within the XML document.
jinesh sam 10-Nov-15 2:02am    
Dude... can you pls identify why my code breaks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900