using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Diagnostics;

namespace WP7SerializeToXML
{
    public partial class MainPage : PhoneApplicationPage
    {
        private List<Person> personen;

        public MainPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

            personen = new List<Person>();

            Person p1 = new Person();
            p1.Alter = 32;
            p1.Name = "Mustermann";
            p1.Vorname = "Max";
            personen.Add(p1);

            p1 = new Person();
            p1.Alter = 33;
            p1.Name = "Mustermann";
            p1.Vorname = "Maxine";
            personen.Add(p1);

            p1 = new Person();
            p1.Alter = 31;
            p1.Name = "Mustermann";
            p1.Vorname = "Moritz";
            personen.Add(p1);
            if (IsolatedStorageFile.GetUserStoreForApplication().FileExists("test.xml"))
            {
                IsolatedStorageFile.GetUserStoreForApplication().DeleteFile("test.xml");    
            }

            IsolatedStorageFileStream fs =
                IsolatedStorageFile.GetUserStoreForApplication().CreateFile("test.xml");

            DataContractSerializer dcs = new DataContractSerializer(typeof(List<Person>));
            dcs.WriteObject(fs, personen);
            fs.Flush();
            fs.Seek(0, System.IO.SeekOrigin.Begin);
            personen = null;
            personen = (List<Person>)dcs.ReadObject(fs);
            fs.Close();
        }
    }
    [DataContract]
    public class Person
    {
        [DataMember]
        public int Alter;
        [DataMember]
        public string Name;
        [DataMember]
        public string Vorname;
    }
}