|
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
<xx atty="3" attz="0"> and in case it requires a different search method I would also like to know how to obtain the value at node
<vars var0="3" var1="0">0</vars>
?
Anxious to learn how to do this
Best Regards
Tom
="1.0"="utf-8"
<data device="xx" version="1">
<node1>
<xx atty="2" attz="3">
<max>0</max>
<min>0</min>
</xx>
<xx atty="3" attz="0">
<max>0</max>
<min>0</min>
</xx>
<xx atty="3" attz="1">
<max>0</max>
<min>0</min>
</xx>
</node1>
<node2>
<vars var0="0" var1="0">0</vars>
<vars var0="1" var1="0">0</vars>
<vars var0="2" var1="0">0</vars>
<vars var0="3" var1="0">0</vars>
<vars var0="4" var1="0">0</vars>
</node2>
</data>
|
|
|
|
|
LINQ 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.
|
|
|
|
|
How about this: //xx[@atty='3' and @attz='0']/*[name()='min' or name()='max']
|
|
|
|
|
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");
_xmlNavi.MoveToRoot();
_xmlNavi.MoveTo(_xmlNavi.SelectSingleNode("WindowConfig"));
if (FormName != "") { _xmlNavi.MoveTo(_xmlNavi.SelectSingleNode(FormName)); }
_xmlNavi.MoveTo(_xmlNavi.SelectSingleNode(ParentName));
if (_xmlNavi.MoveTo(_xmlNavi.SelectSingleNode(ControlName)))
{
_xmlNavi.MoveTo(_xmlNavi.SelectSingleNode(ControlName));
_xmlNavi.MoveToFirstAttribute();
do
{
if (_xmlNavi.Value != "")
{
_dt.Rows.Add(_xmlNavi.Name, _xmlNavi.Value);
}
} 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: added the comments for more Explanation
Ist probably not the best and most comfortable solution but int works for me, optimizations appreciated
if(this.signature != "")
{
MessageBox.Show("This is my signature: " + Environment.NewLine + signature);
}
else
{
MessageBox.Show("404-Signature not found");
}
modified 5-Dec-13 10:13am.
|
|
|
|
|
Thanks gentlemen..this looks like a great starting point! Appreciate it
|
|
|
|