如何存储&检索XML树的特定部分?

时间:2012-02-02 18:18:13

标签: c# wpf xml linq-to-xml

我有 TreeView ,它绑定到 XML文档。每个 TreeViewItem 都有一个 CheckBox (与Windows目录树一样,但每个项目中都有一个复选框)。

例如这棵树:

enter image description here

我需要将所选项目的路径从root保存到leaf(黄色项目),并从原始树中检索此路径。

那么存储检索此XML树的已检查路径的最佳解决方案是什么?

请注意,我还需要能够比较此路径!

2 个答案:

答案 0 :(得分:0)

如果您想通过XPATh这样做,您可以尝试这样的事情,我希望这就是您所说的..

string xpath = null;
XmlNode configNode = configDom.DocumentElement;
// collect selected nodes in node list
XmlNodeList nodeList =
configNode.SelectNodes(@"//*[@status='checked']");

//MessageBox.Show(Convert.ToString(nodeList.Count));

// loop through nodelist
foreach(XmlNode myNode in nodeList)
{
xpath = GetPathFromNode(myNode); // get node path
xpath = xpath + "\\" + myNode.InnerText.ToString(); // complete path
string
}

答案 1 :(得分:0)

要使用XmlDocument选择节点,请查看SelectSingleNode

相关问题