XML : WPF XML serialization in MVVM and read error

I have for some days now tried to find an example on how to XML serialize in MVVM, without luck. I have done it (almost) without MVVM pattern:

C# class called XMLSaveInputServer

  using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  using System.Threading.Tasks;  using System.Xml.Serialization;  using System.IO;    namespace CIA  {      public class XMLSaveInputServer      {          public static void SaveServerName(object obj, string ServerNameFile)          {              XmlSerializer sr = new XmlSerializer(obj.GetType());              TextWriter write = new StreamWriter(ServerNameFile);              sr.Serialize(write, obj);              write.Close();          }      }  }    

C# class called XMLGetSet

  using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  using System.Threading.Tasks;    namespace CIA  {      public class XLMGetSet      {          private string ServerName;            public string serverName          {              get { return ServerName; }              set { ServerName = value; }          }        }  }    

and the window

  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.Xml.Serialization;  using System.IO;    namespace CIA  {      /// <summary>      /// Interaction logic for MainWindow.xaml      /// </summary>      public partial class MainWindow : Window      {          public MainWindow()          {              InitializeComponent();          }            private void ButtonSaveServerName_Click(object sender, RoutedEventArgs e)          {              try              {                  XLMGetSet getset = new XLMGetSet();                  getset.serverName = TextBoxServerName.Text;                  XMLSaveInputServer.SaveServerName(getset, "ServerNameFile.xml");              }                catch (Exception ex)              {                  MessageBox.Show(ex.Message);              }          }            private void XML_load(object sender, EventArgs e)          {              if (File.Exists("ServerNameFile.xml"))              {                  XmlSerializer xs = new XmlSerializer(typeof(XLMGetSet));                  FileStream read = new FileStream("ServerNameFile.xml", FileMode.Open, FileAccess.Read, FileShare.Read);                  XLMGetSet getset = (XLMGetSet)xs.Deserialize(read);                  TextBoxServerName.Text = getset.serverName;              }          }      }  }    

When i press the button "ButtonSaveServerName" it does create the xml file, and does insert the value from the textbox to the file. however when i close the window and open it again the value are not stored nor read. I dont get any errors, but simply does not display anything in the textbox.

Also can anyone explain to me or demonstrate with my code on how to create this with MVVM pattern. I am really trying my best, but have hard time understand MVVM in pratictice, and would make alot easier if my own code was used.

thanks in advanced

No comments:

Post a Comment