当前位置:编程学习 > XML/UML >>

序列化和LINQ to XML处理XML

整理了两种读写XML文件的方法:序列化和LINQ to XML。WPF写了个简单的例子。


1,MainWindow.xaml源码

[csharp]
<Window x:Class="XmlDemo.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="241" Width="388"> 
    <Grid> 
        <Button Content="加载" HorizontalAlignment="Left" Margin="56,49,0,0" VerticalAlignment="Top" Width="87" Height="39"  Click="Button_Click_1"/> 
        <Button Content="保存" HorizontalAlignment="Left" Margin="215,49,0,0" VerticalAlignment="Top" Width="88" Height="39" Click="Button_Click_2"/> 
        <Button Content="加载" HorizontalAlignment="Left" Margin="56,144,0,0" VerticalAlignment="Top" Width="87" Height="37" Click="Button_Click_3"/> 
        <Button Content="保存" HorizontalAlignment="Left" Margin="215,144,0,0" VerticalAlignment="Top" Width="88" Height="37" Click="Button_Click_4"/> 
        <Label Content="序列化和反序列化方式" HorizontalAlignment="Left" Margin="56,10,0,0" VerticalAlignment="Top"/> 
        <Label Content="LINQ to XML 方式" HorizontalAlignment="Left" Margin="56,103,0,0" VerticalAlignment="Top"/> 
 
    </Grid> 
</Window> 
2,MainWindow.xaml.cs后台代码
[csharp]
/// <summary> 
/// MainWindow.xaml 的交互逻辑 
/// </summary> 
public partial class MainWindow : Window 

    private ClassDemo demo = new ClassDemo(); 
 
    public MainWindow() 
    { 
        InitializeComponent(); 
 
        demo.Name = "myname"; 
        demo.Members = new List<string> { "成员一", "成员二" }; 
 
        demo.Children = new List<ClassChild>{ 
                               new ClassChild(){ID="1",Name="Child 1"}, 
                               new ClassChild(){ID="2",Name="Child 2"}}; 
    } 
 
    /// <summary> 
    /// 反序列化加载 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
        XmlSerializer serializer = new XmlSerializer(typeof(ClassDemo)); 
        StreamReader sr=null; 
 
        try 
        { 
            sr = new StreamReader("XmlDemo.xml"); 
            ClassDemo item = serializer.Deserialize(sr) as ClassDemo; 
 
            MessageBox.Show("加载成功!"); 
        } 
        catch (Exception) 
        { 
            MessageBox.Show("如果文件不存在,请先保存!"); 
        } 
        finally 
        { 
            if (sr != null) 
                sr.Close(); 
        } 
 
    } 
 
    /// <summary> 
    /// 序列化保存 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void Button_Click_2(object sender, RoutedEventArgs e) 
    { 
        XmlSerializer serializer = new XmlSerializer(typeof(ClassDemo)); 
        StreamWriter sw = null; 
 
        try 
        { 
            sw = new StreamWriter("XmlDemo.xml"); 
            serializer.Serialize(sw, demo); 
 
            MessageBox.Show("保存成功!"); 
        } 
        catch (Exception) 
        { 
 
        } 
        finally 
        { 
            if (sw != null) 
            { 
                sw.Close(); 
            } 
        } 
    } 
 
    /// <summary> 
    /// Linq方式加载 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void Button_Click_3(object sender, RoutedEventArgs e)&n

补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,