Ajay
Ajay

Reputation: 6590

How to write data in to Xml File Windows Phone?

I want to create a XML file dynamically. After creating the file, I want to create a following structure in XML file by coding in c# windows phone.

<Contacts> 
   <Contact>
       <Name>ABC</Name>
       <PhoneNumber>1234</PhoneNumber>
       <Email>[email protected]</Email>
   </Contact>
</Contacts> 

I want to insert these type of data in XML file. I have searched on it and I found this example add data to existing xml file using linq. But I am not able to access XDocument in my windows phone project. I have also added System.XML assembly.

So, How can I insert data in XML file? Is it possible in windows phone?

Upvotes: 1

Views: 4827

Answers (1)

anderZubi
anderZubi

Reputation: 6424

XDocument is in System.Xml.Linq namespace. So, at the top of your code file, add:

using System.Xml.Linq;

Then you can write the data to your file the following way:

XDocument xDoc = XDocument.Load("file.xml");
var contactsElement = new XElement("Contacts", 
                             new XElement("Contact",
                                  new XElement("Name", "ABC"),
                                  new XElement("PhoneNumber", "1234"),
                                  new XElement("Email", "[email protected]")));
 xDoc.Add(contactsElement);
 xDoc.Save(...);

Upvotes: 2

Related Questions