CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2009
    Posts
    2

    xml get value from a node

    Hello!

    I have a xml file with a specific data node like this:

    <settings>mean</settings>
    <out class="cell" size="[1,2]">
    <tr>
    <td class="numeric" size="[0,0]" />
    <td class="numeric" size="[17,1]">
    1.46590270801;
    1.47197105847;
    1.44441393964;
    (more values...)
    </td
    </tr>
    </out>
    <pred class="cell" size="[1,2]">
    <tr>
    <td class="numeric" size="[0,0]" />
    <td class="numeric" size="[17,1]">
    110.401;
    1.474;
    (more values...)
    </td>
    </tr>
    </pred>

    Using this code:

    Código: [Seleccione]
    String xmlString = textBox4.Text;
    XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(xmlString));

    while (reader.Read())
    {
    XmlNodeType nodeType = reader.NodeType;
    if (nodeType == XmlNodeType.Element)
    {
    switch (reader.Name)
    {
    case "out":
    textBox5.Text = reader.ReadString();
    break;
    }
    }
    }


    If I ask for the values of out the textBox5 is empty, but if I ask for the "settings" value node I obtain the correct result -> "mean".

    How can I obtain the values from out, and only the first one 1.46590270801; ?

    Regards

  2. #2
    Join Date
    Mar 2007
    Posts
    90

    Re: xml get value from a node

    I think you should use the XPathNavigator.SelectSingleNode to select the values in the element with the size attribute different than "[0,0]".
    Something like this:
    Code:
    //out/tr/td[@size!="[0,0]"]
    To get the first value I think you should parse the InnerXml property of the returned value with String.Split.

    For more information about XPath read the W3Schools XPath Tutorial.

  3. #3
    Join Date
    Oct 2009
    Posts
    2

    Thumbs up Re: xml get value from a node

    Ok, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured