treeview节点写入xml文件并从xml加载到treview

treeview节点写入xml文件并从xml加载到treview

本文介绍了如何将WPF treeview节点写入xml文件并从xml加载到treview c#?(Serialize and deserialise)??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wpf treeview节点中有treeview是文件夹,我想在xml中存储treeview节点 在下次午餐时我需要将xml检索到树视图(序列化和反序列化)。

I have treeview in wpf treeview nodes are folder, I want to store the treeview node in xml  and while next lunching i need to retrieve xml to treeview(serializing and deserializing).

如何做到这一点。谢谢

推荐答案

这是一个反序列化方法  ;供你参考。

Here is a deserializing method for your reference.

#Code背后

public TreeViewWithXmlser()
        {
            InitializeComponent();
            Init();
        }
        public XmlDataProvider DataProvider { get; set; }

        private void Init()
        {
            DataProvider = new XmlDataProvider();
            DataProvider.Source = new Uri(@"D:\Project\WPF\WpfApp\WpfApp1\Treeviewdemo.xml");
            DataProvider.XPath = "/Root";
            this.treeview1.DataContext = DataProvider;
        }

#Xaml

<Window x:Class="WpfApp1.TreeViewWithXmlser"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="TreeViewWithXmlser" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TreeView Grid.Row="0" Name="treeview1" ItemsSource="{Binding XPath=Node}" >
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding XPath=Node}">
                    <TextBlock Text="{Binding XPath=@Name}" Tag="{Binding XPath=@ID}"/>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
        <Button Grid.Row="1" Content="ExportToXml" Click="Button_Click"/>
    </Grid>
</Window>

序列化 方法,如果我完成演示,我会在这里更新

serializing method, I will update here if i complete the demo

此致,

张龙


这篇关于如何将WPF treeview节点写入xml文件并从xml加载到treview c#?(Serialize and deserialise)??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:25