Почитать за это класс XmlSerializer можно тут.
Я пробовал MainWindow сериализовать ничего не вышло. Есть видео по этой сериализации, я его внизу приведу, там для WinForm.
Приведу код та и все по файлам
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<Window x:Class="Serialize.MainWindow" 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:Serialize" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="112" Margin="31,197,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="216"/> <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="367,72,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/> <TextBox x:Name="textBoxOne" HorizontalAlignment="Left" Height="23" Margin="126,48,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/> <Label x:Name="labelOne" Content="One" HorizontalAlignment="Left" Margin="45,48,0,0" VerticalAlignment="Top"/> <TextBox x:Name="textBoxTwo" HorizontalAlignment="Left" Height="26" Margin="126,96,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/> <Label x:Name="labelTwo" Content="two" HorizontalAlignment="Left" Margin="45,96,0,0" VerticalAlignment="Top"/> <TextBox x:Name="textBoxThree" HorizontalAlignment="Left" Height="24" Margin="126,148,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/> <Label x:Name="labelThree" Content="three" HorizontalAlignment="Left" Margin="45,148,0,0" VerticalAlignment="Top"/> </Grid> </Window> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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.IO; using System.Xml.Serialization; namespace Serialize { /// <summary> /// Логика взаимодействия для MainWindow.xaml /// </summary> public partial class MainWindow : Window { Settings m_settings = null; public MainWindow() { InitializeComponent(); //инициализация m_settings = Settings.GetSettings(); //вывод textBoxOne.Text = m_settings.One; textBoxTwo.Text = m_settings.Two; textBoxThree.Text = m_settings.Three; textBox.Text = "One=" + m_settings.One + "\nTwo=" + m_settings.Two + "\nThree=" + m_settings.Three; } ~MainWindow() { //делаем сериализацию m_settings.Save(); } private void button_Click(object sender, RoutedEventArgs e) { m_settings.One = textBoxOne.Text; m_settings.Two = textBoxTwo.Text; m_settings.Three = textBoxThree.Text; textBox.Text = "One=" + m_settings.One + "\nTwo=" + m_settings.Two + "\nThree=" + m_settings.Three; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml.Serialization; namespace Serialize { public class Settings { public string One; public string Two; public string Three; public static Settings GetSettings() { Settings settings = null; string filename = Globals.serializeFile; if (File.Exists(filename)) { using (FileStream fs = new FileStream(filename, FileMode.Open)) { XmlSerializer xser = new XmlSerializer(typeof(Settings)); settings = (Settings)xser.Deserialize(fs); fs.Close(); } } else settings = new Settings(); return settings; } public void Save() { string filename = Globals.serializeFile; if (File.Exists(filename)) File.Delete(filename); using (FileStream fs = new FileStream(filename, FileMode.Create)) { XmlSerializer xser = new XmlSerializer(typeof(Settings)); xser.Serialize(fs, this); fs.Close(); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Serialize { public class Globals { public static string serializeFile = "data.xml"; } } |
И видео с ютуба на эту тему, я его смотрел и сделал себе такую же сериализацию
[youtube]https://www.youtube.com/watch?v=zv7ZkQwDEqM[/youtube]