将 XML 解析为 Treeview 列表 [英] Parse XML to Treeview List

查看:22
本文介绍了将 XML 解析为 Treeview 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Before you ask, yes I've researched about answers regarding my question about XML but I feel like even though I found some useful ones or very close to my case, I have yet to find that spot-on answer.

顺便说一句,我使用的是 Microsoft Visual Studio 2005 (C#).[Windows 申请表]

BTW, I'm using Microsoft visual Studio 2005 (C#). [Windows Application Forms]

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

My problem: to parse XML (Filename.vsysvar) and show the list by GUI. Specific problem: I'm not too familiar with the .vsysvar file extension, will these work with XML? Personal problem: XML is foreign to me, I'm still trying to learn it.

所以我尝试将 .vsysvar 文件保存为 .xml 文件,我找到了 XML TreeView 列表的代码:

So I've tried saving the .vsysvar file as an .xml file and I found this code for XML TreeView listing:

 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.由于篇幅较长且字数有限,以下是文件预览:

I first tried it of course with the sample.xml and it worked, when I tried now my own .xml converted from .vsysvar, that's where the problem starts. I'm not sure if I'm allowed to post this, but here's the File.vsysvar. Since it's quite long and characters are limited, here's the preview of the file:

<?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 格式列表,但列表中写的是代码而不是我想看到的:名称.

What's written in the code above is also shown in the WinForm as it is. Although it follows the TreeView format listing, what's written in the listing are the codes and not what I wanted to see: the names.

想要的结构是这样的:

Category A
    Property_A_1
    Property_A_2
    Property_A_3
Category B
    Property_B_1
    Property_B_2
    Property_B_3

dbc 帮了大忙,我几乎清理了 TreeView 列表.

dbc has been a great help, I've almost cleaned the TreeView listing.

我明白了:

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

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

Am I doing it wrong or am I lacking something or what? Thanks.

推荐答案

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

The existing code displays the entire XML of a "leaf" XML node but just the element name of a non-leaf node. If you don't want that, you need to modify AddNode to display the content you want:

    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 中的第一个命名空间"节点的名称为空,这就是为什么仍然显示全文.)

(The first "namespace" node in your XML has an empty name, which is why the full text is still showing.)

更新

既然您已经展示了您想要实现的 UI,您需要做的是:

Now that you have shown the UI you want to achieve, what you need to do is to:

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

给出

这篇关于将 XML 解析为 Treeview 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆