【发布时间】:2015-02-28 05:00:14
【问题描述】:
我正在使用 VS 2005 C#,正在处理未完成的 WinForm。
我已将 XML 解析为树视图列表,但遇到了一些问题。 我想知道是否有一种方法可以隐藏/过滤/删除名称中包含“_this_text”的某个节点,而无需依赖文本框。
这就是我为这个程序所做的(感谢那些帮助我开发这个程序的人):
#region "***** XML Parsing *****"
private void Form1_Load_1(object sender, EventArgs e)
{
// Initialize the controls and the form.
//label1.Text = "File Path";
textBox2.Text = Application.StartupPath + "\\Continental.vsysvar";
//button6.Text = "XML";
//this.Text = "Software Validation";
}
private TreeNode selectedNode = null;
private void button6_Click(object sender, EventArgs e)
{
try
{
// SECTION 1. Create a DOM Document and load the XML data into it.
XmlDocument dom = new XmlDocument();
dom.Load(textBox2.Text);
// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
/*treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];*/
foreach (XmlNode node in dom.DocumentElement.ChildNodes)
{
if (node.Name == "namespace" && node.ChildNodes.Count == 0 && string.IsNullOrEmpty(GetAttributeText(node, "name")))
continue;
AddNode(treeView1.Nodes, node);
}
treeView1.ExpandAll();
}
/* catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}*/
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void LoadTreeFromXmlDocument(XmlDocument dom)
{
try
{
// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
// SECTION 3. Populate the TreeView with the DOM nodes.
foreach (XmlNode node in dom.DocumentElement.ChildNodes)
{
if (node.Name == "namespace" && node.ChildNodes.Count == 0 && string.IsNullOrEmpty(GetAttributeText(node, "name")))
continue;
AddNode(treeView1.Nodes, node);
}
treeView1.ExpandAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
static string GetAttributeText(XmlNode inXmlNode, string name)
{
XmlAttribute attr = (inXmlNode.Attributes == null ? null : inXmlNode.Attributes[name]);
return attr == null ? null : attr.Value;
}
private void AddNode(TreeNodeCollection nodes, XmlNode inXmlNode)
{
if (inXmlNode.HasChildNodes)
{
string text = GetAttributeText(inXmlNode, "name");
if (string.IsNullOrEmpty(text))
text = inXmlNode.Name;
TreeNode newNode = nodes.Add(text);
XmlNodeList nodeList = inXmlNode.ChildNodes;
for (int i = 0; i <= nodeList.Count - 1; i++)
{
XmlNode xNode = inXmlNode.ChildNodes[i];
AddNode(newNode.Nodes, xNode);
}
}
else
{
// If the node has an attribute "name", use that. Otherwise display the entire text of the node.
string text = GetAttributeText(inXmlNode, "name");
if (string.IsNullOrEmpty(text))
text = (inXmlNode.OuterXml).Trim();
TreeNode newNode = nodes.Add(text);
}
}
#endregion
这里是 XMl 文件的摘录,因为它很长:
<?xml version="1.0" encoding="utf-8"?>
<systemvariables version="4">
<namespace name="" comment="">
<namespace name="_01_Test_Preparation" comment="">
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_02_Shipping_Status_Check" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_02_Shipping_Status_Check_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_01_Get_Dem_ID" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_01_Get_Dem_ID_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_04_ECU_Version_Check_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_03_Test_Run_Init" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_04_ECU_Version_Check" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_05_DEM_Reader" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_03_Test_Run_Init_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
<variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_01_05_DEM_Reader_start" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
</namespace>
我真正想要完成的是隐藏或过滤在其名称中找到包含此文本“_start”的节点。因此,所有名称中包含“_this_start”的节点都将对用户隐藏。我已经读到,从技术上讲,启用或禁用可见性是不可能的,而是将文本变灰等等。
提前谢谢你。
【问题讨论】:
-
它们会再次出现吗?还是他们永远隐藏?从 XML 构建树的过程应该不会太昂贵。因此,除非它很大或经常更改,否则我会考虑每次都重建整个树,不包括“不可见”的节点。
-
它应该对用户隐藏,但实际上不能从 xml 文件本身中删除。我尝试删除名称中包含“_start”的变量,而外部程序无法正常运行。所以同名的变量只有那个区别,功能相同但不能单独操作,显然必须成对。
标签: c# winforms xml-parsing treeview