Click to See Complete Forum and Search --> : xml get value from a node


chromatinpt
October 23rd, 2009, 07:51 AM
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

someuser77
October 23rd, 2009, 01:54 PM
I think you should use the XPathNavigator (http://msdn.microsoft.com/en-us/library/4x3f76ta.aspx).SelectSingleNode (http://msdn.microsoft.com/en-us/library/50wkzt09.aspx) to select the values in the element with the size attribute different than "[0,0]".
Something like this:
//out/tr/td[@size!="[0,0]"]

To get the first value I think you should parse the InnerXml (http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator.innerxml.aspx) property of the returned value with String.Split (http://msdn.microsoft.com/en-us/library/b873y76a.aspx).

For more information about XPath read the W3Schools XPath Tutorial (http://www.w3schools.com/xpath/default.asp).

chromatinpt
October 23rd, 2009, 06:01 PM
Ok, thanks!