c#从我的XML文件中填充树视图,同时过滤它以避免重复
本文关键字:过滤 XML 我的 文件 视图 填充 | 更新日期: 2023-09-27 18:11:44
我有以下XML文件,当文档加载到我的程序中时,它以编程方式填充。
我想要做的是使用XML文件的"system","subsystem"answers"sub子系统"元素创建一个树视图层次结构。
另外,在我的XML文件中会有很多"system"或"subsystem"等元素重复出现的情况,所以我不希望这些在我的treeView中重复出现。我需要一个树视图包含所有的系统,子系统和子子系统的数字,但不重复自己。
<DMs>
<dataModule>
<DMC>DMC-AJ-A-21-21-63-00ZZZ-254Z-B_001-00.XML</DMC>
<techName>Filter assy - blowing</techName>
<infoName>Clean mechanically</infoName>
<system>65</system>
<subsystem>32</subsystem>
<subsubsystem>12</subsubsystem>
<status>Checked In</status>
<notes>-</notes>
<currentUser>-</currentUser>
<validator>-</validator>
<dateMod>-</dateMod>
<size>-</size>
</dataModule>
<dataModule>
<DMC>DMC-xx-A-21-21-63-00ZZZ-041Z-D_001-00.XML</DMC>
<techName>Filter assy</techName>
<infoName>Description of how it is made</infoName>
<system>21</system>
<subsystem>15</subsystem>
<subsubsystem>63</subsubsystem>
<status>Checked In</status>
<notes>-</notes>
<currentUser>-</currentUser>
<validator>-</validator>
<dateMod>-</dateMod>
<size>-</size>
</dataModule>
<dataModule>
<DMC>DMC-xx-A-21-21-63-00ZZZ-042Z-D_001-00.XML</DMC>
<techName>Filter assy - blowing</techName>
<infoName>Description of function</infoName>
<system>21</system>
<subsystem>15</subsystem>
<subsubsystem>63</subsubsystem>
<status>Checked In</status>
<notes>-</notes>
<currentUser>-</currentUser>
<validator>-</validator>
<dateMod>-</dateMod>
<size>-</size>
</dataModule>
<dataModule>
<DMC>DMC-xx-A-21-21-63-00ZZZ-066Z-D_001-00.XML</DMC>
<techName>Filter assy - blowing</techName>
<infoName>Support equipment and tools data</infoName>
<system>32</system>
<subsystem>10</subsystem>
<subsubsystem>01</subsubsystem>
<status>Checked In</status>
<notes>-</notes>
<currentUser>-</currentUser>
<validator>-</validator>
<dateMod>-</dateMod>
<size>-</size>
</dataModule>
</DMs>
我有以下的方法,我认为我在正确的方向,但它得到的系统号码进入一个集合,并对它们进行排序传递给我的方法,我正在努力知道如何做。
public void buildTreeView(string sys, string subsys, string subsubsys)
{
try
{
TreeNode system = treeView1.Nodes.Add("System");
system.Nodes.Add(sys);
TreeNode subsystem = system.Nodes.Add("Subsystem");
subsystem.Nodes.Add(subsys);
TreeNode subsubsystem = subsystem.Nodes.Add("Subsubsystem");
subsubsystem.Nodes.Add(subsubsys);
}
catch (Exception e)
{
MessageBox.Show("error" + e);
}
}
感谢您的帮助。
我希望最终得到的是一个章节编号系统,我可以在treeView中向下钻取,即
+ System - 65
+ Subsystem - 32
Subsystem - 68
+ Subsubsystem - 01
Subsubsystem - 02
+ System - 70
+ Subsystem - 22
Subsystem - 30
+ Subsubsystem - 01
所以我有一个工作版本的代码,照顾你的需求。诀窍是检查节点是否已经存在并添加到节点中,而不是每次都创建一个新节点。我们分步骤生成树视图
指出:
treeView1
为类型为System.Windows.Forms.TreeView
的对象
/// <summary>
/// Initalizes Tree View
/// </summary>
private void InitializeTreeView()
{
var mappingList = ReadXmlData();
BuildTree(mappingList);
}
/// <summary>
/// Reads Mappings from xml file to a List
/// </summary>
/// <returns></returns>
private List<Mapping> ReadXmlData()
{
var mappingList = new List<Mapping>();
var reader = new XmlTextReader(@"D:'data.xml");
while (reader.Read())
{
reader.ReadToFollowing("system");
string system = reader.ReadString();
reader.ReadToFollowing("subsystem");
string subSystem = reader.ReadString();
reader.ReadToFollowing("subsubsystem");
string subSubSystem = reader.ReadString();
mappingList.Add(new Mapping() { _system = system, _subSystem = subSystem, _subSubSystem = subSubSystem });
}
return mappingList;
}
/// <summary>
/// Builds Tree
/// </summary>
/// <param name="mappingList"></param>
private void BuildTree(List<Mapping> mappingList)
{
foreach (var mapping in mappingList)
{
var systemNode = FindNode(mapping._system, NodeType.System);
if (string.IsNullOrEmpty(systemNode.Text))
{
string nodeText = NodeType.System.ToString() + " - " + mapping._system; ;
systemNode.Text = nodeText;
systemNode.Name = nodeText;
treeView1.Nodes.Add(systemNode);
}
var subSystemNode = FindNode(mapping._subSystem, NodeType.SubSystem);
if (string.IsNullOrEmpty(subSystemNode.Text))
{
string nodeText = NodeType.SubSystem.ToString() + " - " + mapping._subSystem;
subSystemNode.Text = nodeText;
subSystemNode.Name = nodeText;
systemNode.Nodes.Add(subSystemNode);
}
var subSubSystemNode = FindNode(mapping._subSubSystem, NodeType.SubSubSystem);
if (string.IsNullOrEmpty(subSubSystemNode.Text))
{
string nodeText = NodeType.SubSubSystem.ToString() + " - " + mapping._subSubSystem;
subSubSystemNode.Text = nodeText;
subSubSystemNode.Name = nodeText;
subSystemNode.Nodes.Add(subSubSystemNode);
}
}
}
/// <summary>
/// Returns an existing node from the treeview other-wise returns a new node
/// </summary>
/// <param name="data"></param>
/// <param name="type"></param>
/// <returns></returns>
private TreeNode FindNode(string data, NodeType type)
{
string text = type.ToString() + " - " + data;
if (treeView1.Nodes.Find(text, true).FirstOrDefault() != null)
{
return treeView1.Nodes.Find(text, true).FirstOrDefault();
}
else
{
return new TreeNode();
}
}
/// <summary>
/// Class for Mapping
/// </summary>
public class Mapping
{
public string _system;
public string _subSystem;
public string _subSubSystem;
}
/// <summary>
/// Type of Node
/// </summary>
public enum NodeType
{
System = 0,
SubSystem = 1,
SubSubSystem = 2
}