我在链接“http://msdn.microsoft.com/en-us/library/windows/desktop/ms762271(v=vs.85).aspx”中提供了一个名为“books.xml”的xml文件。 。我的要求是在树视图中仅将<title>
从xml信息中删除为节点。但是当我进行以下编码时,它将所有值显示为像“目录”这样的节点作为rootnode,将所有作者的父节点预订为作为节点的作者,标题,类型等,但我只想要根节点目录和标题作为节点甚至不是书本。任何机构都可以指导我在exisitng逻辑中将标题显示为节点时需要做哪些修改
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();
}
答案 0 :(得分:1)
我假设你的意图只是在类别节点下显示标题而不是其他内容。在这种情况下,请尝试以下版本的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));
}
然而,我必须强调,这是实现目标的非常低效和不优雅的方式。实际上,您可以使用XPath表达式执行此操作,如下所示:
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
}
}}