Advertisement
Guest User

Untitled

a guest
Apr 25th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. I have made it-
  2. MainWindow.xaml
  3. <Window x:Class="SerializeDeSerialize.MainWindow"
  4. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6. xmlns:Nikita="clr-namespace:SerializeDeSerialize"
  7. Title="MainWindow" Height="350" Width="525" Closed="Window_Closed_1" Loaded="Window_Loaded_1">
  8. <Window.DataContext>
  9. <Nikita:Movie></Nikita:Movie>
  10. </Window.DataContext>
  11. <Grid>
  12. <Grid.RowDefinitions>
  13. <RowDefinition></RowDefinition>
  14. <RowDefinition></RowDefinition>
  15. <RowDefinition></RowDefinition>
  16. <RowDefinition></RowDefinition>
  17. <RowDefinition></RowDefinition>
  18. </Grid.RowDefinitions>
  19. <Grid.ColumnDefinitions>
  20. <ColumnDefinition></ColumnDefinition>
  21. <ColumnDefinition></ColumnDefinition>
  22.  
  23. </Grid.ColumnDefinitions>
  24.  
  25. <Label Name="lblTitle" Content="Title:" Grid.Row="0" Grid.Column="0" Margin="10,25,63,10" HorizontalAlignment="Left" Width="164"></Label>
  26. <TextBox Grid.Row="0" Name="txtTitle" Grid.Column="1" Margin="10,25,63,10" Text="{Binding Title}"></TextBox>
  27. <Label Grid.Row="1" Name="lblRating" Content="Rating:" Grid.Column="0" Margin="10,25,63,10" HorizontalAlignment="Left" Width="164"></Label>
  28. <TextBox Grid.Row="1" Name="txtRating" Grid.Column="1" Margin="10,25,63,10" Text="{Binding Rating}"></TextBox>
  29. <Label Grid.Row="2" Name="lblReleaseDate" Content="Release Date:" Grid.Column="0" Margin="10,25,63,10" HorizontalAlignment="Left" Width="164"></Label>
  30. <TextBox Grid.Row="2" Name="txtReleaseDate" Grid.Column="1" Margin="10,25,63,10" Text="{Binding ReleaseDate}"></TextBox>
  31.  
  32. <Button Grid.Row="3" Content="Window1" Margin="66,36,69,10" Click="Button_Click_1"></Button>
  33. <Button Grid.Row="4" Content="Window2" Margin="66,36,69,10" Click="Button_Click_2"></Button>
  34.  
  35. </Grid>
  36. </Window>
  37.  
  38. MainWindow.xaml.cs
  39.  
  40.  
  41. using System;
  42. using System.Collections.Generic;
  43. using System.IO;
  44. using System.Linq;
  45. using System.Text;
  46. using System.Windows;
  47. using System.Windows.Controls;
  48. using System.Windows.Data;
  49. using System.Windows.Documents;
  50. using System.Windows.Input;
  51. using System.Windows.Media;
  52. using System.Windows.Media.Imaging;
  53. using System.Windows.Navigation;
  54. using System.Windows.Shapes;
  55. using System.Xml.Serialization;
  56.  
  57. namespace SerializeDeSerialize
  58. {
  59. /// <summary>
  60. /// Interaction logic for MainWindow.xaml
  61. /// </summary>
  62. public partial class MainWindow : Window
  63. {
  64. SerializeDeSerialize.Movie mv = new Movie();
  65. static Movie mm = new Movie();
  66.  
  67. public MainWindow()
  68. {
  69. InitializeComponent();
  70.  
  71. DataContext = mv;
  72.  
  73. }
  74.  
  75. private void Window_Closed_1(object sender, EventArgs e)
  76. {
  77. SerializeToXML(mv);
  78. }
  79.  
  80. static public void SerializeToXML(Movie movie)
  81. {
  82. XmlSerializer serializer = new XmlSerializer(typeof(Movie));
  83. TextWriter textWriter = new StreamWriter(@"C:Users398780DesktopNikitamovie.xml");
  84. serializer.Serialize(textWriter, movie);
  85. textWriter.Close();
  86. }
  87.  
  88. private void Window_Loaded_1(object sender, RoutedEventArgs e)
  89. {
  90. mm=DeserializeFromXML();
  91. txtTitle.Text = mm.Title;
  92. txtRating.Text =Convert.ToString(mm.Rating);
  93. txtReleaseDate.Text = Convert.ToString(mm.ReleaseDate);
  94.  
  95. }
  96.  
  97. static Movie DeserializeFromXML()
  98. {
  99. XmlSerializer deserializer = new XmlSerializer(typeof(Movie));
  100. TextReader textReader = new StreamReader(@"C:Users398780DesktopNikitamovie.xml");
  101. mm=(Movie)deserializer.Deserialize(textReader);
  102.  
  103. textReader.Close();
  104.  
  105. return mm;
  106. }
  107.  
  108. private void Button_Click_1(object sender, RoutedEventArgs e)
  109. {
  110. Window1 win1 = new Window1();
  111. win1.Show();
  112. }
  113.  
  114. private void Button_Click_2(object sender, RoutedEventArgs e)
  115. {
  116. Window2 win2 = new Window2();
  117. win2.Show();
  118. }
  119. }
  120. }
  121.  
  122. MVVM-movie.cs
  123.  
  124. using System;
  125. using System.Collections.Generic;
  126. using System.Linq;
  127. using System.Text;
  128.  
  129. namespace SerializeDeSerialize
  130. {
  131. public class Movie
  132. {
  133. private string _title;
  134. private int _rating;
  135. private DateTime _releaseDate;
  136.  
  137. public Movie()
  138. {
  139. }
  140. public string Title
  141. {
  142. get { return _title; }
  143. set { _title = value; }
  144. }
  145.  
  146. public int Rating
  147. {
  148. get { return _rating; }
  149. set { _rating = value; }
  150. }
  151.  
  152. public DateTime ReleaseDate
  153. {
  154. get { return _releaseDate; }
  155. set { _releaseDate = value; }
  156. }
  157. }
  158. }
  159.  
  160. Please check..it is same as you told or not?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement