把已知的XML文档读进treeview中,同时在propertygrid中显示各节点的属性分不多,但先谢过各位大大了··^_^
解决方案 »
- 高分求助:C#调用C++编写的dll以实现串口数据收发
- c# linq to xml 查询问题
- REF参数和OUT参数是什么意识?
- 有谁用过Grove,能说说使用Access数据库行不?
- WebBrowser怎么用代码跳转到里面的一个连接
- 我有一个C#中删除代码,但是运行有出错,请大家帮助指教一下谢谢!
- System.Configuration.ConfigurationSettings.AppSettings取值的问题
- 做过对IIS操作的朋友请帮帮忙
- 谁帮我解释一下这段代码中的using有什么作用?
- 哪里可以下载到C# primer
- 哪位高手可以告诉俺,在WEB下如何动态设置权限?
- 100分求助:如何把16进制数转化为相应的子符啊?
{
try
{
this.Cursor = System.Windows.Forms.Cursors.WaitCursor; string strXPath = "XML/I140";
string strRootNode = "Treeview Sample";
string strXMLFile = @"C:\...\test.xml"; // Load the XML file.
XmlDocument dom = new XmlDocument();
dom.Load(strXMLFile); // Load the XML into the TreeView.
this.treeView1.Nodes.Clear();
this.treeView1.Nodes.Add(new TreeNode(strRootNode));
TreeNode tNode = new TreeNode();
tNode = this.treeView1.Nodes[0]; XmlNodeList oNodes = dom.SelectNodes(strXPath);
XmlNode xNode = oNodes.Item(0).ParentNode; AddNode(ref xNode, ref tNode); this.treeView1.CollapseAll();
this.treeView1.Nodes[0].Expand();
this.Cursor = System.Windows.Forms.Cursors.Default;
} catch (Exception ex)
{
this.Cursor = System.Windows.Forms.Cursors.Default;
MessageBox.Show(ex.Message, "Error");
}
}private void AddNode(ref XmlNode inXmlNode, ref TreeNode inTreeNode)
{
// Recursive routine to walk the XML DOM and add its nodes to a TreeView.
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i; // Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[i];
AddNode(ref xNode, ref tNode);
}
}
else
{
inTreeNode.Text = inXmlNode.OuterXml.Trim();
}
}private void btnExpand_Click(object sender, System.EventArgs e)
{
if (this.btnExpand.Text == "Expand All Nodes")
{
this.treeView1.ExpandAll();
this.btnExpand.Text = "Collapse All Nodes";
}
else
{
this.treeView1.CollapseAll();
this.treeView1.Nodes[0].Expand();
this.btnExpand.Text = "Expand All Nodes";
}
}
http://www.codeproject.com/cs/miscctrl/XmlGridControl.asp