从列表框选择的父节点获取子节点的内容 [英] Get tha content of a child node from the parent selected by the listbox
问题描述
大家好,我会问您是否可以执行以下任务:
Hi everybody, i would ask you if it's possible to do the following task:
这是我的xml文件:
<跨度类= "标签">< SETTING_LIST> 跨度> <跨度类="pln">
< setting>
< name> 首先 < / name>
< enable> 命令... < / enable>
< disable> 该命令。 .. < / disable>
< check> ; 命令... < / check>
< w ord> 首先 < / word>
跨度> <跨度类= "标签">< ifchecktrue> 跨度> <跨度类= "PLN">真跨度> <跨度类= "标签">< / ifchecktrue> 跨度> <跨度class ="pln">
< usegrep> true < / usegrep>
< / setting>
< setting>
< ; name> second < / name>
< enable> 命令... < / enable>
< disable> 命令... < / disable>
< check> 命令... < / check>
< word> first < / word>
< ; ifchecktrue> 跨度> <跨度类= "PLN">真跨度> <跨度类= "标签">< / ifchecktrue> 跨度> <跨度类= "PLN">
< usegrep> true < / usegrep> < span class ="pln">
< / setting>
< / SETTING_LIST>
<SETTING_LIST>
<setting>
<name>first</name>
<enable>the command...</enable>
<disable>the command...</disable>
<check>the command...</check>
<word>first</word>
<ifchecktrue>true</ifchecktrue>
<usegrep>true</usegrep>
</setting>
<setting>
<name>second</name>
<enable>the command...</enable>
<disable>the command...</disable>
<check>the command...</check>
<word>first</word>
<ifchecktrue>true</ifchecktrue>
<usegrep>true</usegrep>
</setting>
</SETTING_LIST>
因此用户将从列表框中选择父节点的名称,例如"first"和"first"。或"第二",而不是我需要的东西,将内容作为子节点的字符串返回给我。我尝试过类似的东西,但它不起作用。
so the user will select from a listbox the name of the parent node like "first" or "second", than i'd need something that give me back the content as string of the child node . I tried something like this but it doesen't work.
private void checkbtn_Click(object sender,EventArgs e)
  ;&NBSP;&NBSP;&NBSP; {
string selectednode = Convert.ToString(listBox1.SelectedItem);
字符串提示;
XmlDocument doc = new XmlDocument();
doc.Load(" dati.txt");
$
XmlNode的节点= doc.SelectSingleNode(QUOT;设定/" + selectednode + QUOT; /检查");
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; prompt = node.InnerText;
$
MessageBox.Show(提示);
$
}
private void checkbtn_Click(object sender, EventArgs e)
{
string selectednode = Convert.ToString(listBox1.SelectedItem);
string prompt;
XmlDocument doc = new XmlDocument();
doc.Load("dati.txt");
XmlNode node = doc.SelectSingleNode("setting/" + selectednode + "/check");
prompt = node.InnerText;
MessageBox.Show(prompt);
}
推荐答案
试试这个:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("../../XMLFile1.xml");
XmlNode node = xmlDoc.SelectSingleNode("/SETTING_LIST/setting[name='first']");
foreach (XmlNode nd in node)
{
richTextBox1.Text += nd.OuterXml + "\n";
}
另请参阅以下类似文件:
Also please refer to the following similar documents:
如何:查找同级节点(XPath-LINQ to XML)(C#)
问候,
Stanly
这篇关于从列表框选择的父节点获取子节点的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!