【问题标题】:Reading an XML file into a TreeView将 XML 文件读入 TreeView
【发布时间】:2023-03-08 18:25:01
【问题描述】:

我正在尝试使用 TreeView 控件将 XML 文件加载到我的 GUI 上。 但是,我正在为我的 XML 文件使用专有布局。

XML 的结构如下:

<ConfiguratorConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Section>
        <Class ID="Example" Name="CompanyName.Example" Assembly="Example.dll">
            <Interface>
                <Property Name="exampleProperty1" Value="exampleValue" />
                <Property Name="exampleProperty2" Value="exampleValue" />
                <Property Name="exampleProperty3" Value="exampleValue" />
            </Interface>
        </Class>
    </Section>
</ConfiguratorConfig>

我希望输出的结构如下:

Class "Example"
    Property "exampleProperty1"
    Property "exampleProperty2"
    Property "exampleProperty3"

我对使用 XML 完全陌生。过去几个小时我一直在网上搜索,但没有任何结果有帮助。有些已经接近了,但可能属性不会显示,或者节点的名称不会显示,等等。

我在 Visual Studio 2005 中使用 c# 编写。 感谢您的帮助!

【问题讨论】:

    标签: c# xml treeview visual-studio-2005


    【解决方案1】:

    您可以使用 XmlDocument 遍历节点,您可以将此演示放在控制台应用程序的 Main 方法中:

            string xml = @"<ConfiguratorConfig xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
    <Section>
        <Class ID=""Example"" Name=""CompanyName.Example"" Assembly=""Example.dll"">
            <Interface>
                <Property Name=""exampleProperty1"" Value=""exampleValue"" />
                <Property Name=""exampleProperty2"" Value=""exampleValue"" />
                <Property Name=""exampleProperty3"" Value=""exampleValue"" />
            </Interface>
        </Class>
    </Section></ConfiguratorConfig>";
    
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
    
            foreach (XmlNode _class in doc.SelectNodes(@"/ConfiguratorConfig/Section/Class"))
            {
                string name = _class.Attributes["ID"].Value;
                Console.WriteLine(name);
    
                foreach (XmlElement element in _class.SelectNodes(@"Interface/Property"))
                {
                    if (element.HasAttribute("Name"))
                    {
                        string nameAttributeValue = element.Attributes["Name"].Value;
                        Console.WriteLine(nameAttributeValue);
                    }
                }
            }
    

    如果您使用的 .NET 版本高于 3.0,则可以使用 XDocument 类(如果可以选择,建议使用)。

            XDocument xdoc = XDocument.Parse(xml);
            foreach (XElement _class in xdoc.Descendants("Class"))
            {
                string name = _class.Attribute("ID").Value;
                Console.WriteLine(name);
    
                foreach (XElement element in _class.Descendants("Property"))
                {
                    XAttribute attributeValue = element.Attribute("Name");
                    if (attributeValue != null)
                    {
                        string nameAttributeValue = attributeValue.Value;
                        Console.WriteLine(nameAttributeValue);
                    }
                }
            }
    

    【讨论】:

    • 完美!我设法调整了这段代码来填充我的 TreeView,这正是我所需要的。
    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多