Serializing a Data Object into XML : Xml Data « Data « Silverlight






Serializing a Data Object into XML

   

<UserControl x:Class='SilverlightApplication3.MainPage'
    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' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'>
    <Grid x:Name="LayoutRoot" Background="LightBlue">
       <TextBox x:Name = "NameText" Text="Full Name"/>
       <TextBox x:Name = "EmailText" Text="Email Address"/>
       <Button x:Name="SerializeBtn" Content="Serialize"/>
       <Button x:Name="DeSerializeBtn" Content="Deserialize"/>
       <ScrollViewer Background="White">
           <TextBlock x:Name="myText"/>
        </ScrollViewer>
    </Grid>
 </UserControl>
//File:Page.xaml.cs

  using System;
  using System.Collections.Generic;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Documents;
  using System.Windows.Input;

  using System.Xml.Serialization;
  using System.IO.IsolatedStorage;
  using System.IO;

   namespace SilverlightApplication3
  {
     public partial class MainPage : UserControl
     {
        IsolatedStorageFile localStore;
        IsolatedStorageFileStream localStream;

         public MainPage()
         {
             InitializeComponent();

             localStore  = IsolatedStorageFile.GetUserStoreForApplication();

             SerializeBtn.Click += new RoutedEventHandler(doSerialize);
             DeSerializeBtn.Click += new RoutedEventHandler(doDeSerialize);
           }

          void doSerialize(object sender, RoutedEventArgs e)
          {
             localStream = new IsolatedStorageFileStream("contact.xml",FileMode.Create,localStore);

             ContactData contact = new ContactData();
             contact.Name = NameText.Text;
             contact.Email = EmailText.Text;
             XmlSerializer serializer = new XmlSerializer(contact.GetType());
             serializer.Serialize(localStream, contact);
             localStream.Close();

             localStream = new IsolatedStorageFileStream("contact.xml",FileMode.Open,localStore);
             StreamReader sReader = new StreamReader(localStream);
             myText.Text = sReader.ReadToEnd();
             sReader.Close();
             localStream.Close();
         }

         void doDeSerialize(object sender, RoutedEventArgs e)
         {
             localStream = new IsolatedStorageFileStream("contact.dat",FileMode.Open,localStore);
             ContactData contact = new ContactData();
             XmlSerializer serializer = new XmlSerializer(contact.GetType());
             contact = serializer.Deserialize(localStream) as ContactData;
             localStream.Close();
             myText.Text = String.Format("Full Name: {0}\r\nEmail: {1}",contact.Name, contact.Email);
         }
     }

     [XmlRoot("ContactsXml")]
     public class ContactData
     {
          public ContactData()
          {
          }
          private string FullName = "";
          [XmlElement("FullName")]
          public string Name{
             get { return this.FullName; }
             set { this.FullName = value; }
          }

         private string EmailAddr = "";
         [XmlElement("Email")]
         public string Email
         {
             get { return this.EmailAddr; }
             set { this.EmailAddr = value; }
         }
    }
}

   
    
    
  








Related examples in the same category

1.Accessing XML Data with an XmlReaderAccessing XML Data with an XmlReader
2.Using XmlReader and XmlWriter
3.Read xmlRead xml