Предыдущий пост -
Следующий пост -

C# WPF сериализация XmlSerializer класс

Рубрика: С#, Дата: 4 October, 2015, Автор:

Почитать за это класс XmlSerializer можно тут.

Я пробовал MainWindow сериализовать ничего не вышло. Есть видео по этой сериализации, я его внизу приведу, там для WinForm.

Приведу код та и все по файлам

MainWindow.xaml »

<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>

MainWindow.cs »

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;
        }
    }
}

Settings.cs »

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();
            }
        }
    }
}

Globals.cs »

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]

 

Комментарии:


Оставить комментарий

Your email address will not be published. Required fields are marked *