C# 从XML填充treeview
我目前正在尝试从web请求返回给我的xml填充treeview。当响应传入时,我正在处理数据,以便XML位于此布局中:C# 从XML填充treeview,c#,xml,treeview,treenode,C#,Xml,Treeview,Treenode,我目前正在尝试从web请求返回给我的xml填充treeview。当响应传入时,我正在处理数据,以便XML位于此布局中: <GroupList> <Group> <GroupName>my first test group</GroupName> <GroupID>djnsldgnljsdngljsdngljns</GroupID> <AccessLevel>
<GroupList>
<Group>
<GroupName>my first test group</GroupName>
<GroupID>djnsldgnljsdngljsdngljns</GroupID>
<AccessLevel>high</AccessLevel>
<SubGroup>
<SubGroupName>my first test subgroup</SubGroupName>
<SubGroupID>djnsldgnljsdngljsdngljns</SubGroupID>
</SubGroup>
</Group>
<Group>
<GroupName>my second test group</GroupName>
<GroupID>djnsldgnljsdngljsdngl</GroupID>
<AccessLevel>high</AccessLevel>
<SubGroup>
<SubGroupName>my second test subgroup</SubGroupName>
<SubGroupID>DBXRdjnsldgnljsdngljsdngl</SubGroupID>
</SubGroup>
<SubGroup>
<SubGroupName>my second test subgroup1</SubGroupName>
<SubGroupID>EJdjnsldgnljsdngljsdngl42</SubGroupID>
</SubGroup>
</Group>
</GroupList>
我的第一个测试组
djnsldgnljsdngljsdngljns
高的
我的第一个测试子组
djnsldgnljsdngljsdngljns
我的第二个测试组
djnsldgnljsdngljsdngl
高的
我的第二个测试子组
DBXRdjnsldgnljsdngljsdngl
我的第二个测试子组1
EJdjnsldgnljsdngljsdngl42
我要做的就是显示groupName,然后您就可以展开并查看子组了。目前,我已经让它“排序”工作,但它在一个线性视图中的所有。以下是我目前拥有的代码:
xmlDoc.LoadXml(response2);
groupsTreeView.Nodes.Clear();
groupsTreeView.Nodes.Add(new
TreeNode(xmlDoc.DocumentElement.InnerText));
TreeNode tNode = new TreeNode();
tNode = (TreeNode)groupsTreeView.Nodes[0];
addTreeNode(xmlDoc.DocumentElement, tNode);
groupsTreeView.ExpandAll();
//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];
groupsTreeView.Nodes.Add(new TreeNode(xNode.Value));
tNode = groupsTreeView.Nodes[x];
addTreeNode(xNode, tNode);
}
}
else //No children, so add the outer xml (trimming off whitespace)
treeNode.Text = xmlNode.OuterXml.Trim();
}
xmlDoc.LoadXml(response2);
groupsTreeView.Nodes.Clear();
groupsTreeView.Nodes.Add(新建)
TreeNode(xmlDoc.DocumentElement.InnerText));
TreeNode tNode=新的TreeNode();
tNode=(TreeNode)groupsTreeView.Nodes[0];
addTreeNode(xmlDoc.DocumentElement,tNode);
groupsTreeView.ExpandAll();
//递归调用此函数,直到加载所有节点
私有void addTreeNode(XmlNode XmlNode,TreeNode TreeNode)
{
xmlnodexnode;
三烯醇化物;
XmlNodeList xNodeList;
if(xmlNode.HasChildNodes)//当前节点有子节点
{
xNodeList=xmlNode.ChildNodes;
对于(int x=0;x您正确地使用了递归来遍历Xml文件,不幸的是,您在每次迭代中将树节点添加到树视图的根中。相反,请修改代码,将子节点添加到循环中正在处理的树节点中,例如
for (int x = 0; x <= xNodeList.Count - 1; x++)
//Loop through the child nodes
{
xNode = xmlNode.ChildNodes[x];
// Use the treenode, not the treeview!!!
treeNode.Nodes.Add(new TreeNode(xNode.Value));
tNode = groupsTreeView.Nodes[x];
addTreeNode(xNode, tNode);
}
for(int x=0;x您正确地使用递归在Xml文件中移动,不幸的是,您在每次迭代中将树节点添加到树视图的根中。相反,请修改代码,将子节点添加到循环中正在处理的树节点,例如
for (int x = 0; x <= xNodeList.Count - 1; x++)
//Loop through the child nodes
{
xNode = xmlNode.ChildNodes[x];
// Use the treenode, not the treeview!!!
treeNode.Nodes.Add(new TreeNode(xNode.Value));
tNode = groupsTreeView.Nodes[x];
addTreeNode(xNode, tNode);
}
for(int x=0;x您可以使用linq xml(System.xml.linq)尝试使用此选项:
您可以这样称呼它myTreeView.Nodes.Add(TNGroups(groupsXML))
要将XML加载到元素中,只需使用XElement.load
,您可以尝试使用LINQXML(System.XML.linq)来使用它:
您可以这样称呼它myTreeView.Nodes.Add(TNGroups(groupsXML))
要将XML加载到元素中,只需使用XElement.load
我使用了您的应答代码,发现一些具有属性和复杂结构的XML显示错误。混合了您的所有想法就可以得到这个结果。好像对某人有用一样
private void populateBaseNodes(XmlDocument docXML)
{
tView.Nodes.Clear(); // Clear
tView.BeginUpdate();
TreeNode treenode;
XmlNodeList baseNodeList = docXML.ChildNodes;
foreach (XmlNode xmlnode in baseNodeList)
{
string key = xmlnode.Name == null ? "" : xmlnode.Name.ToString();
string value = xmlnode.Value == null ? xmlnode.Name.ToString() : xmlnode.Value.ToString();
treenode = tView.Nodes.Add(key, value); // add it to the tree
if (xmlnode.Attributes.Count > 0)
{
foreach (XmlAttribute att in xmlnode.Attributes)
{
TreeNode tnode = new TreeNode();
string _name = att.Name;
string _value = att.Value.ToString();
tnode.Name= _name;
tnode.ForeColor = Color.Red;
tnode.Text= "<Attribute>:" +_name;
TreeNode _attvalue = new TreeNode();
_attvalue.Name = _name;
_attvalue.Text = _value;
_attvalue.ForeColor = Color.Purple;
tnode.Nodes.Add(_attvalue);
treenode.Nodes.Add(tnode);
}
}
AddChildNodes(xmlnode, treenode);
}
tView.EndUpdate();
tView.Refresh();
}
private void AddChildNodes(XmlNode nodeact, TreeNode TreeNodeAct)
{
XmlNodeList ChildNodeList = nodeact.ChildNodes;
TreeNode aux = null;
if (nodeact.HasChildNodes)
{
//Recursive Call
foreach (XmlNode xmlChildnode in nodeact.ChildNodes)
{
//Add Actual Node & Properties
string Key = xmlChildnode.Name == null ? "" : xmlChildnode.Name.ToString();
string Value = xmlChildnode.Value == null ? xmlChildnode.Name.ToString() : xmlChildnode.Value.ToString();
aux = TreeNodeAct.Nodes.Add(Key, Value);
AddChildNodes(xmlChildnode, aux);
if (xmlChildnode.Attributes != null && xmlChildnode.Attributes.Count > 0)
{
foreach (XmlAttribute att in xmlChildnode.Attributes)
{
TreeNode tnode = new TreeNode();
string _name = att.Name;
string _value = att.Value.ToString();
tnode.Name = _name;
tnode.Text = "<Attribute>:" + _name;
tnode.ForeColor = Color.Red;
tnode.Text = "<Attribute>:" + _name;
TreeNode _attvalue = new TreeNode();
_attvalue.Name = _name;
_attvalue.Text = _value;
_attvalue.ForeColor = Color.Purple;
tnode.Nodes.Add(_attvalue);
aux.Nodes.Add(tnode);
}
}
}
}
}
private void populateBaseNodes(XmlDocument docXML)
{
tView.Nodes.Clear();//清除
tView.BeginUpdate();
三烯醇三烯醇;
XmlNodeList baseNodeList=docXML.ChildNodes;
foreach(baseNodeList中的XmlNode XmlNode)
{
字符串key=xmlnode.Name==null?”:xmlnode.Name.ToString();
字符串值=xmlnode.value==null?xmlnode.Name.ToString():xmlnode.value.ToString();
treenode=tView.Nodes.Add(key,value);//将其添加到树中
如果(xmlnode.Attributes.Count>0)
{
foreach(xmlnode.Attributes中的xmldattribute att)
{
TreeNode tnode=新的TreeNode();
字符串_name=附件名称;
字符串_value=att.value.ToString();
tnode.Name=\u Name;
tnode.ForeColor=颜色为红色;
tnode.Text=“:”+_名称;
TreeNode _attvalue=新的TreeNode();
_attvalue.Name=\u Name;
_文本=_值;
_attvalue.ForeColor=颜色.紫色;
tnode.Nodes.Add(_attvalue);
treenode.Nodes.Add(tnode);
}
}
AddChildNodes(xmlnode,treenode);
}
EndUpdate();
tView.Refresh();
}
私有void AddChildNodes(XmlNode nodeact、TreeNode TreeNodeAct)
{
XmlNodeList ChildNodeList=nodeact.ChildNodes;
TreeNode aux=null;
if(nodeact.HasChildNodes)
{
//递归调用
foreach(nodeact.ChildNodes中的XmlNode xmlChildnode)
{
//添加实际节点和属性(&P)
字符串Key=xmlChildnode.Name==null?”:xmlChildnode.Name.ToString();
字符串值=xmlChildnode.Value==null?xmlChildnode.Name.ToString():xmlChildnode.Value.ToString();
aux=TreeNodeAct.Nodes.Add(键,值);
AddChildNodes(xmlChildnode,aux);
if(xmlChildnode.Attributes!=null&&xmlChildnode.Attributes.Count>0)
{
foreach(xmlChildnode.Attributes中的xmldattribute att)
{
TreeNode tnode=新的TreeNode();
字符串_name=附件名称;
字符串_value=att.value.ToString();
tnode.Name=\u Name;
tnode.Text=“:”+_名称;
tnode.ForeColor=颜色为红色;
tnode.Text=“:”+_名称;
TreeNode _attvalue=新的TreeNode();
_attvalue.Name=\u Name;
_文本=_值;
_attvalue.ForeColor=颜色.紫色;
tnode.Nodes.Add(_attvalue);
辅助节点添加(tnode);
}
}
}
}
}
我使用了你的回答代码,发现一些带有属性和复杂结构的XML显示错误。混合了你所有的想法,得出了这个结论。似乎可以用于
using System;
using System.Text;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
namespace TreeViewTest
{
class XmlTreeViewBuilder
{
private XmlDocument xDoc;
private TreeView tView;
//Constructor with parameters
public XmlTreeViewBuilder(XmlDocument xDocument, TreeView treeView)
{
this.xDoc = xDocument;
this.tView = treeView;
}
public void getTreeView()
{
tView.Nodes.Clear(); //Clear out the nodes before building
XmlNode pNode = xDoc.DocumentElement; //Set the xml parent node = xml document element
string Key = pNode.Name == null ? "" : pNode.Name; //If null set to empty string, else set to name
string Value = pNode.Value == null ? Key : pNode.Value; //If null set to node name, else set to value
TreeNode tNode = tView.Nodes.Add(Key, Value); //Add the node to the Treeview, set tNode to that node
AddTreeNodes(pNode, tNode); //Call the recursive function to build the tree
}
//Build out the tree recursively
private void AddTreeNodes(XmlNode currentParentNode, TreeNode currentTreeNode)
{
//Check to see if the node has attributes, if so add them
if (currentParentNode.Attributes != null && currentParentNode.Attributes.Count > 0)
{
foreach (XmlAttribute attrib in currentParentNode.Attributes)
{
//Create a node for the attribute name
TreeNode attribNode = new TreeNode();
attribNode.Name = attrib.Name;
attribNode.ForeColor = Color.Red;
attribNode.Text = "<Attribute>:" + attrib.Name;
//treeNode adds the attribute node
currentTreeNode.Nodes.Add(attribNode);
//Create a node for the attribute value
TreeNode attribValue = new TreeNode();
attribValue.Name = attrib.Name;
attribValue.ForeColor = Color.Blue;
attribValue.Text = attrib.Value;
//Attribute node adds the value node
attribNode.Nodes.Add(attribValue);
}
}
//Recursively add children, grandchildren, etc...
if (currentParentNode.HasChildNodes)
{
foreach (XmlNode childNode in currentParentNode.ChildNodes)
{
string Key = childNode.Name == null ? "" : childNode.Name;
string Value = childNode.Value == null ? Key : childNode.Value;
TreeNode treeNode = currentTreeNode.Nodes.Add(Key, Value);
//Recursive call to repeat the process for all child nodes which may be parents
AddTreeNodes(childNode, treeNode);
}
}
}
}
}
XmlTreeViewBuilder tBuilder = new XmlTreeViewBuilder(xmlDoc, treeView1);
tBuilder.getTreeView();