Reputation: 213
I am creating a windows 8 phone application, in which i am reading a xml file called User and add want to add the attributes id and name to the user element of the xaml using XDocument.
But I am not getting how to save it back to the xml file.
XDocument doc = XDocument.Load(@"XDocument.Load(@"Assets\User.xml");
XElement element = doc.Element("user");
XAttribute idAtt = new XAttribute("id", userDetails.UserId);
element.Add(idAtt);
XAttribute nameAtt = new XAttribute("name", userDetails.UserName);
element.Add(nameAtt);
Please help.
Upvotes: 0
Views: 733
Reputation: 4593
That's how I save my XML files:
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("User.xml", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(PrivacyDataClass));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, data);
}
}
}
Upvotes: 1