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>

  

C# WPF xml序列化 反序列化的更多相关文章

  1. C# Json库 和 xml 序列化反序列化 存在的问题

    json 正常情况下不会对私有成员进行序列化和反序列化, 因此在用json做深拷贝时, 就会丢失数据. 解决办法:       声明成公有成员. json在序列化和反序列化时, 如果类中有IComma ...

  2. C# XML序列化/反序列化参考

    .NET提供了很不错的XML序列化/反序列化器,(它们所在的命名空间为System.Xml.Serialization)这是很方便的,下面对它的使用做一些总结,以供参考. 1,简单序列化 public ...

  3. php json与xml序列化/反序列化

    在web开发中对象的序列化与反序列化经常使用,比较主流的有json格式与xml格式的序列化与反序列化,今天想写个jsop的小demo,结果发现不会使用php序列化,查了一下资料,做个笔记 简单数组js ...

  4. XML序列化反序列化—常用类

    public class XMLSerializer    {        #region (public) xml序列化        /// <summary>        /// ...

  5. C# 复杂格式多级深度XML序列化反序列化

     default.xml 文件如下: <config><partnerships>  <partnership name="Main_Listener" ...

  6. WPF XML序列化保存数据 支持Datagrid 显示/编辑/添加/删除数据

    XML序列化保存数据 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

  7. C# XML序列化/反序列化类XmlSerializer使用示例

    using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; ...

  8. XML序列化反序列化

    using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; nam ...

  9. XML序列化/反序列化数据库形式保存和读取。

    直接上码: 首先创建class1类 public class Class1 { public string name { get; set; } public int age { get; set; ...

随机推荐

  1. FI配置步骤清单

    1.定义公司代码   配置路径: R/3定制IMG的实施指南>企业结构>定义>财务会计>定义, 复制, 删除, 检查公司代码 事务代码 EC01 2.编辑科目表清单   配置路 ...

  2. 腾讯TBS X5 WebView的简单使用

    工作中经常涉及H5网页的加载工作,最多使用的就是安卓系统控件WebView,但是当网页内容比较多的时候,需要等待很久才能加载完,加载完后用户才能看到网页中的内容,这样用户需要等很久,体验很差. 那能不 ...

  3. Python+Selenium笔记(十六)屏幕截图

    (一) 方法 方法 简单说明 save_screenshot(filename)   获取当前屏幕截图并保存为指定文件 filename:路径/文件名 get_screenshot_as_base64 ...

  4. python 遇到的一些坑

    lst = [1, 2, 4] print lst.__iter__().next() # 打印出来的是 1 print lst.__iter__().next() # 打印出来的是 1 # 调用__ ...

  5. LeetCode题解之Copy List with Random Pointer

    1.题目描述 2.问题分析 首先要完成一个普通的单链表的深度复制,然后将一个旧的单链表和新的单链表的节点使用map对应起来,最后,做一次遍历即可. 3.代码 RandomListNode *copyR ...

  6. PHP用正则匹配字符串中的特殊字符防SQL注入

    本文出至:新太潮流网络博客 /** * [用正则匹配字符串中的特殊字符] * @E-mial wuliqiang_aa@163.com * @TIME 2017-04-07 * @WEB http:/ ...

  7. SQL Server全文搜索

    SQL Server全文搜索 看这篇文章之前请先看一下下面我摘抄的全文搜索的MSDN资料,基本上MSDN上关于全文搜索的资料的我都copy下来了 并且非常认真地阅读和试验了一次,并且补充了一些SQL语 ...

  8. SQL 中常用存储过程xp_cmdshell运行cmd命令 (转载)

    目的:使用SQL语句,在D盘创建一个文件夹myfile 首先查询系统配置 SELECT * FROM sys.configurations WHERE name='xp_cmdshell' OR na ...

  9. leveldb源码阅读

    http://blog.csdn.net/sparkliang/article/details/8567602 http://brg-liuwei.github.io/tech/2014/10/15/ ...

  10. C Programming vs. Java Programming

    Thing C Java type of language function oriented object oriented basic programming unit function clas ...