C# WPF xml序列化 反序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;
using System.IO;
namespace Test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
            dataGrid1.DataContext = list;
          //  dataGrid1.ItemsSource = list;

        }
         List<msgitem> list = new List<msgitem>();
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            loadMsgItems();
        }



        void loadMsgItems() {

            dataGrid1.ItemsSource = null;
            list = (List<msgitem>)LoadFromXml("d:\1.xml", list.GetType());
            dataGrid1.ItemsSource = list;

          //  list.Add(new msgitem() { isSelect=false, msg="test ..d第三方第三方?、工业投入也太容易>/....." });
          //  list.Add(new msgitem() { isSelect = true, msg = "test .d第三方第三方?、工业投入也太容&&***//222......." });
          //  dataGrid1.ItemsSource = null ;
          //  dataGrid1.ItemsSource = list;


        
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SaveToXml("d:\1.xml", list);
            dataGrid1.ItemsSource = null;
            list = (List<msgitem>)LoadFromXml("d:\1.xml", list.GetType());
            dataGrid1.ItemsSource = list;
        }

       void SaveToXml(string filePath, object sourceObj )
        {
            if (!string.IsNullOrWhiteSpace(filePath) && sourceObj != null)
            {
       

                using (StreamWriter writer = new StreamWriter(filePath))
                {
                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(sourceObj.GetType());
                    XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();

                    nameSpace.Add("", ""); //not ot output the default namespace
                    xmlSerializer.Serialize(writer, sourceObj, nameSpace);
                }
            }
        }


       object LoadFromXml(string filePath, Type type)
        {
            object result = null;

            if (File.Exists(filePath))
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
                    result = xmlSerializer.Deserialize(reader);
                }
            }
            return result;
        }

       private void CheckBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
       {

       }



    }







}

  

UI:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid Height="215" Margin="23,96,0,0" Name="grid1" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="321*" />
                <ColumnDefinition Width="130*" />
            </Grid.ColumnDefinitions>
            <ListBox  SelectionMode="Single" Margin="6" Name="dataGrid1"  ScrollViewer.VerticalScrollBarVisibility="Auto"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" >

                <ListBox.ItemTemplate>
                    <DataTemplate>
                      
                            <CheckBox  MouseDoubleClick="CheckBox_MouseDoubleClick"  Content="{Binding msg}"  Width="444"  IsChecked="{Binding isSelect}"   />
                         
                     
                    </DataTemplate>
                </ListBox.ItemTemplate>

               

            </ListBox>
        </Grid>
        <Button Content="Button" Height="31" HorizontalAlignment="Left" Margin="28,59,0,0" Name="button1" VerticalAlignment="Top" Width="149" Click="button1_Click" />
    </Grid>
</Window>

  

原文地址:https://www.cnblogs.com/wgscd/p/10172887.html