none
XML Single Node Selection RRS feed

  • Question

  • Hi all,

    I have the below xml file i need to debatch the xml file which means return a single node based on the node no as parameter a selection.

    and i use the following but i got the error the type node does not valid any one can correct my code.

      <?xml version="1.0" encoding="utf-8" ?>
    - <ns0:CustInfo xmlns:ns0="http://testing.test.123">
    - <Customers>
    - <Customer>
      <AccountID>ID1980</AccountID>
      <PartyName>Name1</PartyName>
      </Customer>
    - <Customer>
      <AccountID>ID1981</AccountID>
      <PartyName>Name2</PartyName>
      </Customer>
    - <Customer>
      <AccountID>ID1993</AccountID>
      <PartyName>Name3</PartyName>
      </Customer>
      </Customers>
      </ns0:CustInfo>

    My Code :

     string strPath = string

    .Empty;

     XmlDocument xmlReturnValue = null;

    XmlNamespaceManager nsmgr = new XmlNamespaceManager 

    (xmlDoc.NameTable);

    nsmgr.AddNamespace("ns0", "http://testing.test.123"

    );

     xmlReturnValue =new XmlDocument();

    strPath = xmlDoc.SelectSingleNode("//ns0:CustInfo//ns0:Customers//ns0:Customer[" + rowPath + "]"

    , nsmgr).OuterXml;

    xmlReturnValue.LoadXml(strPath);

    System.Diagnostics.EventLog.WriteEntry("Application", rowPath.ToString());

    return xmlReturnValue;

     

    Desired OUtput 

     <?xml version="1.0" encoding="utf-8" ?>
    - <ns0:CustInfo xmlns:ns0="http://testing.test.123">
    - <Customer>
      <AccountID>ID1980</AccountID>
      <PartyName>Name1</PartyName>
      </Customer>


    Thanks,

    vasantha Prabakaran


    Thanks, Vasantha Prabakaran

    Friday, November 18, 2011 10:22 AM

Answers

  • There are a couple things you got confused.  Here's a complete self-contained example using your data, that works:

    using System;
    using System.Xml;
    
    namespace XmlSelectExperiments
    {
      class Program
      {
        static void Main(string[] args)
        {
          XmlDocument xmlDoc = new XmlDocument();
          xmlDoc.LoadXml(@"
    <ns0:CustInfo xmlns:ns0='http://testing.test.123' xmlns='http://testing.test.123'>
      <Customers>
        <Customer>
          <AccountID>ID1980</AccountID> 
          <PartyName>Name1</PartyName> 
        </Customer>
        <Customer>
          <AccountID>ID1981</AccountID> 
          <PartyName>Name2</PartyName> 
        </Customer>
        <Customer>
          <AccountID>ID1993</AccountID> 
          <PartyName>Name3</PartyName> 
        </Customer>
      </Customers>
    </ns0:CustInfo>
         ");
    
          XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
          nsmgr.AddNamespace("ns0", "http://testing.test.123" );
    
          int row = 1;
          while (true)
          {
            string xPath = "/ns0:CustInfo/ns0:Customers/ns0:Customer[" + row + "]";
            XmlNode ndCustomer = xmlDoc.SelectSingleNode(xPath, nsmgr);
            if (null == ndCustomer)
              break;
            Console.WriteLine(ndCustomer.OuterXml);
            row++;
          }
        }
      }
    }
    

    A couple of things to note:

    • You shouldn't use '//' as a path separate in XPath.  While it will work in many cases, including this one, it's inefficient. 
    • Your XML document uses a namespace on the root node, but not on the interior nodes.  I modified your XML to add a default namespace so that all the nodes are in the same namespace.  If you need the Xml to remain as you posted, then remove the ns0: qualifications from all node names except for CustInfo when composing your XPath.

    -cd Mark the best replies as answers!
    Friday, November 18, 2011 3:26 PM
    Moderator

All replies

  • There are a couple things you got confused.  Here's a complete self-contained example using your data, that works:

    using System;
    using System.Xml;
    
    namespace XmlSelectExperiments
    {
      class Program
      {
        static void Main(string[] args)
        {
          XmlDocument xmlDoc = new XmlDocument();
          xmlDoc.LoadXml(@"
    <ns0:CustInfo xmlns:ns0='http://testing.test.123' xmlns='http://testing.test.123'>
      <Customers>
        <Customer>
          <AccountID>ID1980</AccountID> 
          <PartyName>Name1</PartyName> 
        </Customer>
        <Customer>
          <AccountID>ID1981</AccountID> 
          <PartyName>Name2</PartyName> 
        </Customer>
        <Customer>
          <AccountID>ID1993</AccountID> 
          <PartyName>Name3</PartyName> 
        </Customer>
      </Customers>
    </ns0:CustInfo>
         ");
    
          XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
          nsmgr.AddNamespace("ns0", "http://testing.test.123" );
    
          int row = 1;
          while (true)
          {
            string xPath = "/ns0:CustInfo/ns0:Customers/ns0:Customer[" + row + "]";
            XmlNode ndCustomer = xmlDoc.SelectSingleNode(xPath, nsmgr);
            if (null == ndCustomer)
              break;
            Console.WriteLine(ndCustomer.OuterXml);
            row++;
          }
        }
      }
    }
    

    A couple of things to note:

    • You shouldn't use '//' as a path separate in XPath.  While it will work in many cases, including this one, it's inefficient. 
    • Your XML document uses a namespace on the root node, but not on the interior nodes.  I modified your XML to add a default namespace so that all the nodes are in the same namespace.  If you need the Xml to remain as you posted, then remove the ns0: qualifications from all node names except for CustInfo when composing your XPath.

    -cd Mark the best replies as answers!
    Friday, November 18, 2011 3:26 PM
    Moderator
  • Thanks Carl.,
    Thanks, Vasantha Prabakaran
    Monday, November 28, 2011 8:59 AM