select single node question

Last post 07-01-2009 2:04 PM by Martin_Honnen. 5 replies.

Sort Posts:

  • select single node question

    07-01-2009, 12:37 PM
    • Member
      7 point Member
    • tpiazza55
    • Member since 09-29-2008, 8:06 PM
    • Posts 84
    how do i do a selectsinglenode to get to the name = 1 node when i have to use Attributes.GetNamedItem("name")
     
    <lst name="counts">
        <lst name="fields">
             <lst name="ID">
                  <int name="1">14</int>
             <lst>
        <lst>
    <lst>


  • Re: select single node question

    07-01-2009, 12:52 PM

     What does SelectSingleNode have to do with Attributes.GetNamedItem? I don't understand your question.

    If you want to find the first element where the name attribute has the value 1 then use

    XmlElement el = xmlDocumentInstance.SelectSingleNode("//*[@name = 1]") as XmlElement;

    If you want to find the first int element where the name attribute has the value 1 then use

    XmlElement el = xmlDocumentInstance.SelectSingleNode("//int[@name = 1]") as XmlElement;

    If that does not help then you need to elaborate why you think you need Attributes.GetNamedItem with SelectSingleNode or what exactly you want to achieve.

    Martin Honnen --- MVP XML
    My blog
  • Re: select single node question

    07-01-2009, 1:19 PM
    • Member
      7 point Member
    • tpiazza55
    • Member since 09-29-2008, 8:06 PM
    • Posts 84

     

  • <lst name="counts">   
  •     <lst name="fields">   
  •          <lst name="ID">   
  •               <int name="1">14</int>   
  •          <lst>   
  •     <lst>   
  • <lst> 
  • what i need to do is get to the name="1" so i can getthe value 14

    XmlElement el = xmlDocumentInstance.SelectSingleNode("//int[@name = 1]") as XmlElement;

    doesnt seem to find it

    the only way i have been able to get to it is using foreach node and drilling down that way

     

  • Re: select single node question

    07-01-2009, 1:36 PM

     Please post the real XML you have and we can find out why the XPath does not find anything. Namespaces might be one reason.

    What you have posted now is not XML at all as you have start tags <lst> which need to be closed as </lst> but you have not done that.

    Martin Honnen --- MVP XML
    My blog
  • Re: select single node question

    07-01-2009, 1:47 PM
    • Member
      7 point Member
    • tpiazza55
    • Member since 09-29-2008, 8:06 PM
    • Posts 84
    <response>
    	<lst name="responseHeader">		   
                              <int name="status">0</int>	  
                              <lst name="params">
    		<str name="rows">50</str>
    	           </lst>
    	</lst>
    </response>
    
    

    i need the vlaue of rows
  • Re: select single node question

    07-01-2009, 2:04 PM
    Answer

     Let's assume the XML you have is

    <response>
      <lst name="responseHeader">
        <int name="status">0</int>
        <lst name="params">
          <str name="rows">50</str>
        </lst>
      </lst>
    </response>
    


     

    then this snippet of C#

                XmlDocument xmlDocumentInstance = new XmlDocument();
                xmlDocumentInstance.Load(@"..\..\XMLFile1.xml");
                XmlElement el = xmlDocumentInstance.SelectSingleNode("//str[@name = 'rows']") as XmlElement;
                if (el != null)
                {
                    Console.WriteLine(el.InnerText);
                }
    


    outputs 50.

    Martin Honnen --- MVP XML
    My blog
Page 1 of 1 (6 items)