Windows Phone
Windows Phone

Reputation: 11

Save xml file Windows phone 7

How can I save xml file (code) ? Hello everybody. I can read the xml file, but I can't save xml file : Sample : Class Subject :

public class Subject 
{
   public int Id 
   { 
     get; 
     set; 
   }
   public Subject() 
   {}
   public string Name 
   { 
     get; 
     set; 
   }
   public string Number 
   {
     get; 
     set; 
   }
}

Class Student :

public class Student 
{
   public string Id 
   { 
      get; 
      set; 
   }
   public Student() 
   {}
   public string Name 
   {
      get; 
      set; 
   }

   public string IconURI 
   {
      get; 
      set; 
   }
   public List<Subject> Subjects 
   {
      get; 
      set; 
   }
}

MainPage.xaml.cs :

public partial class MainPage : PhoneApplicationPage 
{
    public MainPage() 
    {
      InitializeComponent();
    }
    public List<Student> Students 
    {
      get; 
      set; 
    }
}

Load file xml :

private void LoadXML() 
{
   Uri uri = new Uri("/DemoWP7;component/Save.xml", UriKind.Relative); 
   StreamResourceInfo xml = App.GetResourceStream(uri); 
   XDocument doc = XDocument.Load(xml.Stream);
   XElement menu = doc.Descendants("menu").First(); 
   Students = (from student in menu.Descendants("student") 
   select new Student() 
   {
      Id = student.Attribute("id").Value, 
      Name = student.Attribute("name").Value, 
      IconURI = student.Attribute("icon").Value, 
      Subjects = (from subject in student.Descendants("subject") 
      select new Subject() 
      {
         Name = subject.Attribute("name").Value, 
         Number = subject.Attribute("number").Value, 
      }).ToList<Subject>() 
    }).ToList<Student>(); 
}

I need save the file .xml same this, after that LoadXML()

<?xml version="1.0" encoding="utf-8" ?> 
<menu> 
<student id="group1" name="Team A" icon="Images/Team-A.png"> 
<subject name="Math" 
number="72"> 
</subject> 
<subject name="Art" 
number="85"> 
</subject> 
</student>
<student id="group2" name="Team D" icon="Images/Team-D.png"> 
<subject name="History" 
number="69"> 
</subject> 
</student> 

 How can I save xml file (code) ?

Upvotes: 0

Views: 2014

Answers (2)

Henry C
Henry C

Reputation: 4801

Have you considered using the XmlSerializer class, and marking up your Subject class with the attributes on the various properties? It'll make your code a little simpler - a simple example can be found here. (and to save the Xml file you need to write it to the IsolatedStorage for your particular application instance, for which if you do a quick google or look at the other answers you'll find a heap of samples)

Upvotes: 0

Ilya Builuk
Ilya Builuk

Reputation: 2179

For security reasons, each application has own place for files - Isolation Storage. So, if you want to save your file, you should do something like that:

    using(var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
    using(var file = appStorage.OpenFile("YourFileName.xml", FileMode.OpenOrCreate))
    using(var writer = new StreamWriter(file))
    {
        writer.Write("Your data");
    }

Upvotes: 2

Related Questions