none
Is there a way to access child node by name instead of location in XmlDocument?

    Question

  • First I like to say that this is my first post here and I like to apologize if I post in the wrong place (please let me do where is the right place).
    I'm strugleling with this question for several weeks now and all my searches in google, codeproject, ic#code and tons of other forums didn't gave me an answer :(

    I have the following code:


    XmlDocument xdoc = new XmlDocument();
    XmlNode settings = xdoc.FirstChild.FirstChild;
    switch ( settings.ChildNodes.Count )
    {
    default:
    case 3: Settings.NextSerial = Convert.ToInt32(settings.ChildNodes[2].InnerText); goto case 2;
    case 2: Settings.LastUpdate = Convert.ToDateTime(settings.ChildNodes[1].InnerText); goto case 1;
    case 1: Settings.Version = Convert.ToDouble(settings.ChildNodes[0].InnerText); break;
    }

     

    As you can see I read a value stored in an xml document


    <root>
    <Settings>
    <Version>0.1</Version>
    <NextSerial>1</NextSerial>
    <LastUpdate>04/08/2005</LastUpdate>
    </Settings>
    </root>

     

    Now I access the version element by: settings.ChildNodes[0].InnerText

    My question is:

    Is there a way to access the child node by its name instead of bby its location?
    Example:
    settings.ChildNodes["Version"].InnerText

    Ofcourse that example didn't worked because the ChildNodes want only an int value and not a string :(


    **edit how do you do code tags here?**
    Thursday, August 04, 2005 4:00 PM

Answers

  • If the XML is contained in an XmlDocument object, then use XPath to navigate the data an extract the value you need for casting to it's target type, something like:

    XmlNode versionNode = xdoc.SelectSingleNode("/root/Settings/Version");
    double versionNum = 0;
    if(versionNode != null){
       versionNum = double.Parse(versionNode.InnerText);
    }

    Note, the text string "/root/Settings/Version" is the XPath part.

    Hope this helps.
    Thursday, August 04, 2005 5:22 PM
  • Woot got it ;)

    What I do is this:

    I declare a node:
    XmlNode settings = xdoc.FirstChild.FirstChild;

    Then I access the Node by the name "Version" in that node like this:
    settings.SelectSingleNode("Version").InnerText;

    Thank you so much Scott Cadillac, you solved a problem I worked on for several weeks in a matter of minutes ;)


    P.S.
    I still want to know how to use code tags here because I plan on being here for a while ;)
    Thursday, August 04, 2005 6:50 PM

All replies

  • If the XML is contained in an XmlDocument object, then use XPath to navigate the data an extract the value you need for casting to it's target type, something like:

    XmlNode versionNode = xdoc.SelectSingleNode("/root/Settings/Version");
    double versionNum = 0;
    if(versionNode != null){
       versionNum = double.Parse(versionNode.InnerText);
    }

    Note, the text string "/root/Settings/Version" is the XPath part.

    Hope this helps.
    Thursday, August 04, 2005 5:22 PM
  •  Scott Cadillac wrote:
    If the XML is contained in an XmlDocument object, then use XPath to navigate the data an extract the value you need for casting to it's target type, something like:<br /><br /><font face="Courier New" color="#0000ff">XmlNode versionNode = xdoc.SelectSingleNode("/root/Settings/Version");<br />double versionNum&nbsp;= 0;<br />if(versionNode != null){<br />&nbsp;&nbsp;&nbsp;versionNum = double.Parse(versionNode.InnerText);<br />}<br /></font><br />Note, the text string "/root/Settings/Version" is the XPath part.<br /><br />Hope this helps.

    Ok that's a great start ;)
    Now how can I use the path for the node "settings" that I declared before in the example?

    The reason I want to use that path is because in the database I'm creating I'll have several elements of the same name under the root element and each of them will have it's own values.
    So what I do is:
    settings[0].ChildNodes[0].InnerText
    Where settings[0] is the path for the element I need (first settings element) and the ChildNodes[0] is the Version of that element.

    Basically I need a way to do:

    Get location from element I declared as settings.
    Use that location and get the child node called version on that location.
    Thursday, August 04, 2005 6:45 PM
  • Woot got it ;)

    What I do is this:

    I declare a node:
    XmlNode settings = xdoc.FirstChild.FirstChild;

    Then I access the Node by the name "Version" in that node like this:
    settings.SelectSingleNode("Version").InnerText;

    Thank you so much Scott Cadillac, you solved a problem I worked on for several weeks in a matter of minutes ;)


    P.S.
    I still want to know how to use code tags here because I plan on being here for a while ;)
    Thursday, August 04, 2005 6:50 PM
  • xmlNode.Item("NodeName")
    Thursday, August 04, 2005 6:53 PM
  • Hi daat99,

    Then you probably want to use the SelectNodes() method instead of SelectSingleNode().

    SelectNodes() with XPath will return a XmlNodeList object, which is a collection of nodes from which you could do a loop for the values you want.

    You're at a point where you need to spend some timer learning XML, XPath and the various Classes of the System.Xml namespace.

    For speed, if you XML data is very large, then you might also want to investigate an XmlReader approach.

    Hope this helps.

    Thursday, August 04, 2005 6:56 PM
  •  Scott Cadillac wrote:
    Hi daat99,<br /><br />Then you probably want to use the <font face="Courier New" color="#0000ff">SelectNodes()</font> method instead of <font face="Courier New" color="#0000ff">SelectSingleNode().</font><br /><br /><font face="Courier New" color="#0000ff">SelectNodes()</font> with XPath will return a <font face="Courier New" color="#0000ff">XmlNodeList </font>object, which is a collection of nodes from which you could do a loop for the values you want.<br /><br />You're at a point where you need to spend some timer learning XML, XPath and the various Classes of the System.Xml namespace.<br /><br />For speed, if you XML data is very large, then you might also want to investigate an <font face="Courier New" color="#0000ff">XmlReader</font> approach.<br /><br />Hope this helps.<br /><br />

    I do need the SelectSingleNode because I loop through all the parent nodes using for loop and then I need to access a specific named node child of the parent loop I'm currently in.
    I do have a lot to learn indeed and I thank you for helping me solving this issue :)

     DMan1 wrote:
    xmlNode.Item("NodeName")<br />

    I can't seem to locate an Item method\property in the XmlNode class nor visual studio recognize it :(

    However I access it successfully this way inside a for loop:
    Console.WriteLine(nodSettingsIdea.SelectSingleNode("Version").InnerText);
    That provide a list of versions on the console which is exactly what I wanted to work with ;)


    P.S.
    Hate to nag but I realy want to know how to use code tags here :(
    When I did:
    [code]
    code here
    [/code]
    Like I used to it didn't worked :(
    Thursday, August 04, 2005 7:21 PM
  • In reference to the code tags, there are a couple of toolbar buttons, one with the # sign and one with a little VB.  Click one of those for the code tags.  It generates something like this...

    [ code language="c#"][/code]

    (without the space after the first [ )

    Thursday, August 04, 2005 8:50 PM
  • hmm, for some reason I don't see a toolbar :( all I can see is the emotions thingy.

    Let me try:


    testing

     


    ok it works ;) ty :)
    Friday, August 05, 2005 4:41 AM