how to read value at specifix XML node
-
Hello, How can I obtain the value from a specific node in my XML file? For example please consider the xml structure below. How would I for instance obtain "max" and "min" values for node
and in case it requires a different search method I would also like to know how to obtain the value at node
0
? Anxious to learn how to do this Best Regards Tom
0 0 0 0 0 0
0
0
0
0
0 -
Hello, How can I obtain the value from a specific node in my XML file? For example please consider the xml structure below. How would I for instance obtain "max" and "min" values for node
and in case it requires a different search method I would also like to know how to obtain the value at node
0
? Anxious to learn how to do this Best Regards Tom
0 0 0 0 0 0
0
0
0
0
0LINQ is your friend when walking XML files. There are a lot of examples using Linq queries over XML to get data out of the file. I'm too lazy right now to Google it or search here on CP so I'll leave that to you. But that should get you what you want.
-
Hello, How can I obtain the value from a specific node in my XML file? For example please consider the xml structure below. How would I for instance obtain "max" and "min" values for node
and in case it requires a different search method I would also like to know how to obtain the value at node
0
? Anxious to learn how to do this Best Regards Tom
0 0 0 0 0 0
0
0
0
0
0How about this:
//xx[@atty='3' and @attz='0']/*[name()='min' or name()='max']
-
Hello, How can I obtain the value from a specific node in my XML file? For example please consider the xml structure below. How would I for instance obtain "max" and "min" values for node
and in case it requires a different search method I would also like to know how to obtain the value at node
0
? Anxious to learn how to do this Best Regards Tom
0 0 0 0 0 0
0
0
0
0
0Hey bimbambumbum, that's my way of doing it, with this code you'll get the attributes to your specific node. Code is written for .Net Framework 3.5 Additionaly i posted the XML file so you can backtrace the work and derive your code from it. All you need to have set up is the xmlDoc and a xPathNavigator xmlNavi = xmlDoc.createNavi...
public DataTable LoadValueConfig(string ControlName, string ParentName, string FormName)
{
DataTable _dt = new DataTable();
_dt.Columns.Add("Name");
_dt.Columns.Add("Value");//Move to root node \_xmlNavi.MoveToRoot(); //move to windowConfig Node \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode("WindowConfig")); //if node of form exists move to it if (FormName != "") { \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(FormName)); } //if node of parent exists move to it e.g. bsAdminTest \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ParentName)); // if control Name exists move to it e.g. switchButton1 if (\_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ControlName))) { \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ControlName)); //Move to first attribute \_xmlNavi.MoveToFirstAttribute(); // move through all Attributes and save the values into the DataTable do { if (\_xmlNavi.Value != "") { \_dt.Rows.Add(\_xmlNavi.Name, \_xmlNavi.Value); } // while able to move to next Attribute } while(\_xmlNavi.MoveToNextAttribute()); } return \_dt; }
XML File:
<WindowConfig>
<kopiefrmHauptfenster Position="460:21" Size="878:1170" WindowState="Normal">
<checkBoxItem1 chBoxCheck="True" />
<bsAdminTest Active="False">
<switchButton1 switch="False" />
<richTextBoxEx1 Text="" />
</bsAdminTest>
<Project Active="True">
<navigationPane1 CheckedButton="Projekte" Expanded="True" />
<expandablePanel1 Expanded="True" />
</Project>
</kopiefrmHauptfenster>
</WindowConfig>Edit:
-
Hey bimbambumbum, that's my way of doing it, with this code you'll get the attributes to your specific node. Code is written for .Net Framework 3.5 Additionaly i posted the XML file so you can backtrace the work and derive your code from it. All you need to have set up is the xmlDoc and a xPathNavigator xmlNavi = xmlDoc.createNavi...
public DataTable LoadValueConfig(string ControlName, string ParentName, string FormName)
{
DataTable _dt = new DataTable();
_dt.Columns.Add("Name");
_dt.Columns.Add("Value");//Move to root node \_xmlNavi.MoveToRoot(); //move to windowConfig Node \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode("WindowConfig")); //if node of form exists move to it if (FormName != "") { \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(FormName)); } //if node of parent exists move to it e.g. bsAdminTest \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ParentName)); // if control Name exists move to it e.g. switchButton1 if (\_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ControlName))) { \_xmlNavi.MoveTo(\_xmlNavi.SelectSingleNode(ControlName)); //Move to first attribute \_xmlNavi.MoveToFirstAttribute(); // move through all Attributes and save the values into the DataTable do { if (\_xmlNavi.Value != "") { \_dt.Rows.Add(\_xmlNavi.Name, \_xmlNavi.Value); } // while able to move to next Attribute } while(\_xmlNavi.MoveToNextAttribute()); } return \_dt; }
XML File:
<WindowConfig>
<kopiefrmHauptfenster Position="460:21" Size="878:1170" WindowState="Normal">
<checkBoxItem1 chBoxCheck="True" />
<bsAdminTest Active="False">
<switchButton1 switch="False" />
<richTextBoxEx1 Text="" />
</bsAdminTest>
<Project Active="True">
<navigationPane1 CheckedButton="Projekte" Expanded="True" />
<expandablePanel1 Expanded="True" />
</Project>
</kopiefrmHauptfenster>
</WindowConfig>Edit:
Thanks gentlemen..this looks like a great starting point! Appreciate it
-
How about this:
//xx[@atty='3' and @attz='0']/*[name()='min' or name()='max']
Hi Can I please ask you to elaborate on your approach? For example provide an example. I tried this:
XmlDocument d = new XmlDocument(); d.Load("XmlData2.xml"); var tmp = d.SelectSingleNode("//xx\[@atty='3' and @attz='0'\]/\*\[name()='min' or name()='max'\]"); //var tmp = d.SelectSingleNode("//xx\[@atty='3' and @attz='0'\]/\*\[name()='min'\]");
but I don't get the right values when inspecting the tmp variable. Thanks