C# 如何从xml文件中筛选数据,以便在treeview中仅显示选定的节点
我在链接中提供了一个名为“books.xml”的xml文件http://msdn.microsoft.com/en-us/library/windows/desktop/ms762271(v=vs.85).aspx”。我的要求是在树视图中将xml信息中的C# 如何从xml文件中筛选数据,以便在treeview中仅显示选定的节点,c#,xml,visual-studio-2010,treeview,C#,Xml,Visual Studio 2010,Treeview,我在链接中提供了一个名为“books.xml”的xml文件http://msdn.microsoft.com/en-us/library/windows/desktop/ms762271(v=vs.85).aspx”。我的要求是在树视图中将xml信息中的作为节点禁用。但是,当我进行以下编码时,它将所有值显示为节点,例如“catalog”作为根节点,book作为父节点,然后author、title、genre等作为节点,但我只希望根节点catalog和title作为节点,而不是book。有谁能告诉
作为节点禁用。但是,当我进行以下编码时,它将所有值显示为节点,例如“catalog”作为根节点,book作为父节点,然后author、title、genre等作为节点,但我只希望根节点catalog和title作为节点,而不是book。有谁能告诉我,在现有的将标题显示为节点的逻辑中,我需要做哪些修改
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open XML document";
dlg.Filter = "XML Files (*.xml)|*.xml";
dlg.FileName = Application.StartupPath + "\\..\\..\\Sample.xml";
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
//Just a good practice -- change the cursor to a
//wait cursor while the nodes populate
this.Cursor = Cursors.WaitCursor;
//First, we'll load the Xml document
XmlDocument xDoc = new XmlDocument();
xDoc.Load(dlg.FileName);
//Now, clear out the treeview,
//and add the first (root) node
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new
TreeNode(xDoc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = (TreeNode)treeView1.Nodes[0];
//We make a call to addTreeNode,
//where we'll add all of our nodes
addTreeNode(xDoc.DocumentElement, tNode);
//Expand the treeview to show all nodes
treeView1.ExpandAll();
}
catch (XmlException xExc)
//Exception is thrown is there is an error in the Xml
{
MessageBox.Show(xExc.Message);
}
catch (Exception ex) //General exception
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = Cursors.Default; //Change the cursor back
}
}}
//This function is called recursively until all nodes are loaded
private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes) //The current node has children
{
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++)
//Loop through the child nodes
{
xNode = xmlNode.ChildNodes[x];
treeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = treeNode.Nodes[x];
addTreeNode(xNode, tNode);
}
}
else //No children, so add the outer xml (trimming off whitespace)
treeNode.Text = xmlNode.OuterXml.Trim();
}
OpenFileDialog dlg=newopenfiledialog();
dlg.Title=“打开XML文档”;
dlg.Filter=“XML文件(*.XML)|*.XML”;
dlg.FileName=Application.StartupPath+“\\\\\\\\\\\Sample.xml”;
if(dlg.ShowDialog()==DialogResult.OK)
{
尝试
{
//这是一个很好的实践--将光标更改为
//在节点填充时等待光标
this.Cursor=Cursors.WaitCursor;
//首先,我们将加载Xml文档
XmlDocument xDoc=新的XmlDocument();
加载(dlg.FileName);
//现在,清除树景,
//并添加第一个(根)节点
treeView1.Nodes.Clear();
treeView1.Nodes.Add(新建)
TreeNode(xDoc.DocumentElement.Name));
TreeNode tNode=新的TreeNode();
tNode=(TreeNode)treeView1.Nodes[0];
//我们打电话给addTreeNode,
//我们将在其中添加所有节点
addTreeNode(xDoc.DocumentElement,tNode);
//展开树视图以显示所有节点
treeView1.ExpandAll();
}
捕获(XmlException xExc)
//如果Xml中存在错误,则引发异常
{
MessageBox.Show(xExc.Message);
}
catch(Exception ex)//一般异常
{
MessageBox.Show(例如Message);
}
最后
{
this.Cursor=Cursors.Default;//更改回光标
}
}}
//递归调用此函数,直到加载所有节点
私有void addTreeNode(XmlNode XmlNode,TreeNode TreeNode)
{
xmlnodexnode;
三烯醇化物;
XmlNodeList xNodeList;
if(xmlNode.HasChildNodes)//当前节点有子节点
{
xNodeList=xmlNode.ChildNodes;
对于(int x=0;x我假设您的目的只是在类别节点下显示标题,而不是其他内容。在这种情况下,请尝试以下版本的addTreeNode方法:
private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes && xmlNode.Name != "title") //The current node has children
{
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++)
//Loop through the child nodes
{
xNode = xmlNode.ChildNodes[x];
//treeNode.Nodes.Add(new TreeNode(xNode.Name));
//tNode = treeNode.Nodes[x];
addTreeNode(xNode, treeNode);
}
}
else if (xmlNode.Name == "title") //No children, so add the outer xml (trimming off whitespace)
treeNode.Nodes.Add(new TreeNode(xmlNode.InnerText));
}
如果有人重写我现有的代码,那会很有帮助
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open XML document";
dlg.Filter = "XML Files (*.xml)|*.xml";
dlg.FileName = Application.StartupPath + "\\..\\..\\Sample.xml";
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
//Just a good practice -- change the cursor to a
//wait cursor while the nodes populate
this.Cursor = Cursors.WaitCursor;
//First, we'll load the Xml document
XmlDocument xDoc = new XmlDocument();
xDoc.Load(dlg.FileName);
//Now, clear out the treeview,
//and add the first (root) node
treeView1.Nodes.Clear();
TreeNode rootTreeNode = new TreeNode(xDoc.DocumentElement.Name);
treeView1.Nodes.Add(rootTreeNode);
foreach (XmlNode titleNode in xDoc.DocumentElement.SelectNodes(@"//title"))
{
rootTreeNode.Nodes.Add(titleNode.InnerText);
}
treeView1.ExpandAll();
}
catch (XmlException xExc)
//Exception is thrown is there is an error in the Xml
{
MessageBox.Show(xExc.Message);
}
catch (Exception ex) //General exception
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = Cursors.Default; //Change the cursor back
}
}}