c# - 将 XML 解析为 Treeview 列表

标签 c# xml winforms treeview

在你提问之前,是的,我已经研究了有关 XML 问题的答案,但我觉得即使我找到了一些有用的答案或非常接近我的案例,但我还没有找到准确的答案。

顺便说一句,我正在使用 Microsoft Visual Studio 2005 (C#)。 [Windows 申请表]

我的问题:解析 XML (Filename.vsysvar) 并通过 GUI 显示列表。 具体问题:我不太熟悉 .vsysvar 文件扩展名,这些文件可以与 XML 一起使用吗?个人问题:XML 对我来说很陌生,我仍在努力学习它。

因此,我尝试将 .vsysvar 文件另存为 .xml 文件,并找到了 XML TreeView 列表的以下代码:

 private void Form1_Load(object sender, EventArgs e)
    {
        // Initialize the controls and the form.
        label1.Text = "File Path";
        label1.SetBounds(8, 8, 50, 20);
        textBox1.Text = Application.StartupPath + "\\Continental.xml";
        textBox1.SetBounds(64, 8, 256, 20);
        button1.Text = "Populate the TreeView with XML";
        button1.SetBounds(8, 40, 200, 20);
        this.Text = "TreeView control from XML";
        /*this.Width = 336;
        this.Height = 368;
        treeView1.SetBounds(8, 72, 312, 264);*/
    }

    private void button1_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(textBox1.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];

            // SECTION 3. Populate the TreeView with the DOM nodes.
            AddNode(dom.DocumentElement, tNode);
            treeView1.ExpandAll();
        }
        catch (XmlException xmlEx)
        {
            MessageBox.Show(xmlEx.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
    {
        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(xNode, tNode);
            }
        }
        else
        {
            // Here you need to pull the data from the XmlNode based on the
            // type of node, whether attribute values are required, and so forth.
            inTreeNode.Text = (inXmlNode.OuterXml).Trim();
        }
    }

我首先尝试使用sample.xml,它起作用了,当我现在尝试从.vsysvar转换为我自己的.xml时,这就是问题开始的地方。我不确定我是否可以发布此内容,但这是 File.vsysvar。由于它很长并且字符有限,以下是文件的预览:

<?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>
<namespace name="_02_Communication" comment="">
  <variable anlyzLocal="2" readOnly="false" valueSequence="false" unit="" name="_02_04_VCAN_StartLoad" 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="_02_08_XCP_Restbus_RAM_Monitor" 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="_02_01_VCAN_Output_Cyclic" 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="_02_02_VCAN_Input" 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="_02_05_VCAN_Event_Frame" 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="_02_07_VCAN_Failsafe" 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="_02_03_VCAN_Startup" 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="_02_06_VCAN_Manipulate_Input" 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="_02_09_Communication_Min_Max_Voltage" 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="_02_10_Power_On_Mask" 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="_02_11_VCAN_IgnitionOff" 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="_02_12_J1699" 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="_02_13_Input_Data_For_Algo" 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="_02_01_VCAN_Output_Cyclic_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="_02_02_VCAN_Input_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="_02_07_VCAN_Failsafe_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="_02_12_J1699_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="_02_03_VCAN_Startup_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="_02_04_VCAN_StartLoad_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="_02_08_XCP_Restbus_RAM_Monitor_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="_02_05_VCAN_Event_Frame_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="_02_06_VCAN_Manipulate_Input_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="_02_09_Communication_Min_Max_Voltage_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="_02_10_Power_On_Mask_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="_02_11_VCAN_IgnitionOff_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="_02_13_Input_Data_For_Algo_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="_02_14_VCAN_Bus_Off_Dection_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="_02_14_VCAN_Bus_Off_Dection" comment="" bitcount="32" isSigned="true" encoding="65001" type="int" startValue="0" minValue="0" minValuePhys="0" maxValue="4" maxValuePhys="4" />
</namespace>
...
</systemvariables>

上面代码中编写的内容也按原样显示在 WinForm 中。虽然它遵循 TreeView 格式列表,但列表中写的是代码,而不是我想看到的:名称。

所需的结构如下:

Category A
    Property_A_1
    Property_A_2
    Property_A_3
Category B
    Property_B_1
    Property_B_2
    Property_B_3

编辑:dbc 提供了很大的帮助,我几乎清理了 TreeView 列表。

我得到了这个:

systemvariables
    namespace
        Category A
            Property_A_1
            Property_A_2
            Property_A_3
        Category B
            Property_B_1
            Property_B_2
            Property_B_3

是我做错了还是缺少什​​么?谢谢。

最佳答案

现有代码显示“叶”XML 节点的整个 XML,但仅显示非叶节点的元素名称。如果你不想这样,你需要修改AddNode来显示你想要的内容:

    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(XmlNode inXmlNode, TreeNode inTreeNode)
    {
        // Loop through the XML nodes until the leaf is reached.
        // Add the nodes to the TreeView during the looping process.
        if (inXmlNode.HasChildNodes)
        {
            XmlNodeList nodeList = inXmlNode.ChildNodes;
            for (int i = 0; i <= nodeList.Count - 1; i++)
            {
                XmlNode xNode = inXmlNode.ChildNodes[i];
                string text = GetAttributeText(xNode, "name");
                if (string.IsNullOrEmpty(text))
                    text = xNode.Name;
                inTreeNode.Nodes.Add(new TreeNode(text));
                TreeNode tNode = inTreeNode.Nodes[i];
                AddNode(xNode, tNode);
            }
        }
        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();
            if (inTreeNode.Text != text)
                inTreeNode.Text = text;
                inTreeNode.Nodes.Clear();
        }
    }

结果看起来像这样

enter image description here (XML 中的第一个“命名空间”节点的名称为空,这就是仍然显示全文的原因。)

更新

现在您已经显示了想要实现的 UI,您需要做的是:

  1. 跳过根 XML 节点并循环遍历其子节点,为每个节点添加顶级树节点。
  2. 如果顶级“命名空间”节点没有名称和子节点,则跳过该节点。

因此:

    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);
        }
    }

这给出了

enter image description here

关于c# - 将 XML 解析为 Treeview 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28491166/

相关文章:

c# - 多对多关系的 LINQ to SQL 查询

c# - 按下 ENTER 键后提交表格

python - 使用 ElementTree 示例在 Python 中解析 XML

xml - XPath-选择一个名称相同的所有子节点本身都具有特定子节点的节点

使用 css 选择器解析 XML 的 Java 库

c# - 如何为标签做背景没有颜色?

c# - 需要 StatusStrip 从右到左填充其项目

c# - .Net 文件流 - 句柄与路径

c# - 在窗体调整大小时调整控件大小

c# - 从构造函数调用时静态函数意外返回