C# 如何使用c用XML填充TreeView控件#
我是c#新手,我面临的问题是,我有一个XML文件,其中包含一个进程信息列表,其中包含进程id、进程名称和父id字段。我需要帮助将此XML文件填充到树视图中。XML文件的格式不是1。我的XML文件如下所示:C# 如何使用c用XML填充TreeView控件#,c#,xml,C#,Xml,我是c#新手,我面临的问题是,我有一个XML文件,其中包含一个进程信息列表,其中包含进程id、进程名称和父id字段。我需要帮助将此XML文件填充到树视图中。XML文件的格式不是1。我的XML文件如下所示: <Process id="0" name="[System Process]" ParentPID="0" /> <Process id="4" name="System" ParentPID="0" /> <Process id="100" name="MyAp
<Process id="0" name="[System Process]" ParentPID="0" />
<Process id="4" name="System" ParentPID="0" />
<Process id="100" name="MyApp.exe" ParentPID="4" />
<Process id="120" name="avgrsx.exe" ParentPID="10"/>
<Process id="150" name="avgcsrvx.exe" ParentPID="120" />
<Process id="155" name="csrss.exe" ParentPID="100" />
<Process id="170" name="winlogon.exe" ParentPID="100" />
<Process id="180" name="services.exe" ParentPID="170" />
<Process id="200" name="lsass.exe" ParentPID="170" />
<Process id="110" name="svchost.exe" ParentPID="180" />
<Process id="380" name="svchost.exe" ParentPID="200"/>
<Process id="530" name="svchost.exe" ParentPID="1764" />
<Process id="420" name="Avg.exe" ParentPID="110" />
0
|-4
| |-100
| |-155
| |-170
| |-180
| | |-110
| | |-420
| |-200
| |-380
|-10
| |-120
| |-150
|-155
|-1764
|-530
任何帮助都将不胜感激。谢谢尝试以下算法:
TreeNode[] arr = new TreeNode[elemCnt]
arr[0] = new TreeNode with base (root) element
For each XML element "Process":
Let v = Find parent node with linear search in ar
If v is not null ( parent found)
Add element as child to v
Add element to arr
Add nodes that have null parents to TreeView.Nodes
以下是我为您提供的解决方案:
private TreeNode[] FindTreeNodes(string Key)
{
return oTreeView.Nodes.Find(Key, true);
}
private void LoadTreeview(string XmlFile)
{
oTreeView.Nodes.Clear();
if (File.Exists(XmlFile) == true)
{
XmlDocument oXmlDocument = new XmlDocument();
oXmlDocument.Load(XmlFile);
XmlNodeList oXmlNodeList = oXmlDocument.SelectNodes("/Processes/Process");
foreach (XmlNode oXmlNode in oXmlNodeList)
{
int iID = Convert.ToInt32(oXmlNode.Attributes["id"].Value);
string sName = oXmlNode.Attributes["name"].Value;
int iParentID = Convert.ToInt32(oXmlNode.Attributes["ParentPID"].Value);
TreeNode[] oParentNodes = FindTreeNodes(iParentID.ToString());
if (oParentNodes.Length == 0)
{
TreeNode oTreeNode = new TreeNode();
oTreeNode.Name = iID.ToString();
oTreeNode.Text = String.Format("{0} ({1})", sName, iID);
oTreeView.Nodes.Add(oTreeNode);
}
else
{
if (oParentNodes.Length > 0)
{
foreach (TreeNode oParentTreeNode in oParentNodes)
{
TreeNode oTreeNode = new TreeNode();
oTreeNode.Name = iID.ToString();
oTreeNode.Text = String.Format("{0} ({1})", sName, iID);
oParentTreeNode.Nodes.Add(oTreeNode);
}
}
else
{
Console.WriteLine(" ** Could not find the parent node {0} for child {1} ({2})", iParentID, sName, iID);
}
}
}
}
}
protected override void OnLoad(EventArgs E)
{
base.OnLoad(E);
LoadTreeview("Xml.xml");
}
我对XML做了一些修改,使其看起来像这样—本质上是一样的:
<?xml version="1.0" encoding="utf-8" ?>
<Processes>
<Process id="0" name="[System Process]" ParentPID="0" />
<Process id="4" name="System" ParentPID="0" />
<!--
<Process id="10" name="MissingNode" ParentPID="0" />
<Process id="1764" name="MissingNode" ParentPID="0" />
-->
<Process id="100" name="MyApp.exe" ParentPID="4" />
<Process id="120" name="avgrsx.exe" ParentPID="10"/>
<Process id="150" name="avgcsrvx.exe" ParentPID="120" />
<Process id="155" name="csrss.exe" ParentPID="100" />
<Process id="170" name="winlogon.exe" ParentPID="100" />
<Process id="180" name="services.exe" ParentPID="170" />
<Process id="200" name="lsass.exe" ParentPID="170" />
<Process id="110" name="svchost.exe" ParentPID="180" />
<Process id="380" name="svchost.exe" ParentPID="200"/>
<Process id="530" name="svchost.exe" ParentPID="1764" />
<Process id="420" name="Avg.exe" ParentPID="110" />
</Processes>
最后,以下节点不会出现,因为XML中没有它们的定义:
- 十,
- 1764年
干杯 id=10的元素在哪里?您是否有id=10和id=1764的标记?非常感谢您宝贵的回复…我将尝试这种方式,并很快回复您