如何将XML加载到C#WPF树视图中

时间:2019-05-25 14:12:28

标签: c# wpf treeview

我有一个XML文件,其结构为:

<?xml version="1.0" encoding="UTF-8"?>
<PluginMenuLayout DocSchema="1" Name="PluginList">
<Plugin CLSID="{141AC902-4256-4332-4252-455645527E31}" CLSIDMD5="{7286134B-6592-8EE7-6BE8-60A5EC833C10}" Category="1" Name="BREVERB 2 Cakewalk" OriginalName="BREVERB 2 Cakewalk"/>
<Plugin CLSID="{56535454-482D-3374-6833-000000000000}" Category="9" Name="TH3" OriginalName="TH3"/>
<Plugin CLSID="{141AC902-4357-626C-424F-4F5354317E31}" CLSIDMD5="{BB2041A0-618D-98FE-6F9B-F22F6E1A2520}" Category="1" Name="Boost11ртпктеутуетет" OriginalName="Boost11ртпктеутуетет"/>
<Plugin CLSID="{5653544D-6C70-676D-656C-6F64796E6520}" Category="9" Name="Melodyne" OriginalName="Melodyne"/></PluginMenuLayout>

我需要将其加载到TreeView WPF中并从那里显示插件的名称。我尝试这样做:

private void func(){

            DataContext = this;
            TheXML = XDocument.Load(@"C:\Users\Anton\Desktop\plglst.xml");
            myTreeView.DataContext = TheXML;
            myTreeView.UpdateLayout();

        }

        private XDocument _theXML;
        public XDocument TheXML
        {
            get => _theXML;


              set => _theXML = value;
            }
}

但是我的树状视图仅显示类,而不显示名称。我不明白如何使用Treeview和XML。 Here is image of my treeview with loaded XML.

请帮帮我! :)

更新

如何显示带有文件夹和分隔符的示例?

<?xml version="1.0" encoding="UTF-8"?>
<PluginMenuLayout DocSchema="1" Name="newlist">
<Plugin CLSID="{141AC902-4256-4332-4252-455645527E31}" CLSIDMD5="{7286134B-6592-8EE7-6BE8-60A5EC833C10}" Category="1" Name="BREVERB 2 Cakewalk" OriginalName="BREVERB 2 Cakewalk"/>
<Plugin CLSID="{5653544D-6C70-676D-656C-6F64796E6520}" Category="9" Name="Melodyne" OriginalName="Melodyne"/><Plugin CLSID="{56535454-482D-3374-6833-000000000000}" Category="9" Name="TH3" OriginalName="TH3"/>
<Folder Name="Okay">
<Plugin CLSID="{54DE473B-D405-4F77-A19A-995FDC7E374E}" Category="2" Name="Cakewalk TTS-1" OriginalName="Cakewalk TTS-1"/>
<Plugin CLSID="{F718845E-BC87-4248-83C4-A9C99294EA63}" Category="2" Name="GroovePlayer" OriginalName="GroovePlayer"/>
<Separator/>
<Plugin CLSID="{141AC902-4550-4E4F-5349-2D454C457E31}" CLSIDMD5="{9A2FD0E5-C1C5-2926-3FA0-8B45DBEDA75C}" Category="3" Name="SI-Electric Piano" OriginalName="SI-Electric Piano"/>
<Plugin CLSID="{141AC902-5354-5247-5349-2D5354527E31}" CLSIDMD5="{0D9CD314-8203-ACA3-F1BA-7896F2C3BCD7}" Category="3" Name="SI-String Section" OriginalName="SI-String Section"/>
</Folder>
</PluginMenuLayout>

1 个答案:

答案 0 :(得分:2)

欢迎您!

要回答您的问题,应将XmlDocument绑定到ListView的ItemsSource属性,如下所示:

XmlDocument d = new XmlDocument();
d.Load(@"C:\Users\Anton\Desktop\plglst.xml");

var binding = new Binding();
binding.Source = d.DocumentElement;
theListView.SetBinding(ListView.ItemsSourceProperty, binding);

然后在XAML中为要显示的属性声明GridViewCollumns:

<ListView x:Name="theListView">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="CLSID" DisplayMemberBinding="{Binding XPath=@CLSID}"/>
            <GridViewColumn Header="Category" DisplayMemberBinding="{Binding XPath=@Category}"/>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding XPath=@Name}"/>
            <GridViewColumn Header="Original Name" DisplayMemberBinding="{Binding XPath=@OriginalName}"/>
        </GridView>
    </ListView.View>
</ListView>

结果:

enter image description here

说实话,尽管我建议不要像这样直接绑定到XmlDocument。 XML旨在对文档进行编码,一旦您实际上已将数据从磁盘上加载下来,那么将其保持为这种格式几乎不会获得任何收益,而且迟早它会妨碍您的发展。更好的解决方案是创建一个中间数据结构,您可以使用属性来装饰它,以显示XML模式如何映射数据:

public class Plugin
{
    [XmlAttribute("CLSID")]
    public string CLSID { get; set; }

    [XmlAttribute("Category")]
    public string Category { get; set; }

    [XmlAttribute("Name")]
    public string Name { get; set; }

    [XmlAttribute("OriginalName")]
    public string OriginalName { get; set; }
}

您的加载代码现在直接反序列化为类型为Plugin的数组,并将其直接分配给ListView的ItemsSource:

var serializer = new XmlSerializer(typeof(Plugin[]), new XmlRootAttribute { ElementName = "PluginMenuLayout" });
using (XmlReader reader = XmlReader.Create(@"C:\Users\Anton\Desktop\plglst.xml"))
    theListView.ItemsSource = serializer.Deserialize(reader) as Plugin[];

对ListView列绑定进行一些修改即可完成:

<ListView x:Name="theListView">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="CLSID" DisplayMemberBinding="{Binding CLSID}"/>
            <GridViewColumn Header="Category" DisplayMemberBinding="{Binding Category}"/>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Header="Original Name" DisplayMemberBinding="{Binding OriginalName}"/>
        </GridView>
    </ListView.View>
</ListView>