Edward
Edward

Reputation: 7424

Add elements to xml file in WP7?

How do you add an element to an xml file in wp7? I HAVE found alot of sources that show how to add elements in ASP.NET, Silverlight on the browser, etc.. but nothing on wp7. I keep seeing that we're supposed to use XDocument(XML to Linq), just not sure where to start. Thanks.

Upvotes: 1

Views: 2522

Answers (2)

Billy Ray Valentine
Billy Ray Valentine

Reputation: 569

This is how I've been developing for WP7:

using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (var fs = store.OpenFile("MyXmlFile.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite))
    {
        var root = new XElement("Root");
        var someAttribute = new XAttribute("SomeAttribute", "Some Attribute Value");
        var child = new XElement("Child", "Child Value");
        var anotherChild = new XElement("AnotherChild", "Another Child Value");
        var xDoc = new XDocument();
        root.Add(someAttribute, child, anotherChild);
        xDoc.Add(root);
        xDoc.Save(fs);
    }
}

Upvotes: 1

Matt Bridges
Matt Bridges

Reputation: 49425

XDocument usage on WP7 is the same as it is for silverlight. Try something like this:

string xmlStr = "<RootNode><ChildNode>Hello</ChildNode></RootNode>";
XDocument document = XDocument.Parse(xmlStr);
document.Root.Add(new XElement("ChildNode", "World!"));
string newXmlStr = document.ToString(); 
// The value of newXmlStr is now: "<RootNode><ChildNode>Hello</ChildNode><ChildNode>World!</ChildNode></RootNode>"

Upvotes: 3

Related Questions