将 XML 解析为树视图列表

Parse XML to Treeview List

在你问之前,是的,我已经研究了关于 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 并且它成功了,当我现在尝试我自己的 .xml 从 .vsysvar 转换时,这就是问题开始的地方。我不确定我是否可以 post 这个,但这是 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格式的listing,但是listing里面写的是代码,不是我想看的名字。

想要的结构是这样的:

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

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

现有代码显示 "leaf" XML 节点的整个 XML,但仅显示 non-leaf 节点的元素名称。如果你不想这样,你需要修改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();
        }
    }

结果看起来像

(您的 XML 中的第一个 "namespace" 节点名称为空,这就是完整文本仍在显示的原因。)

更新

现在你已经展示了你想要达到的UI,你需要做的是:

  1. 跳过根 XML 节点并遍历其子节点,为每个节点添加顶级树节点。
  2. 如果 top-level "namespace" 节点没有名称和子节点,则跳过该节点。

因此:

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

这给出了