Search code examples
c#xmlsilverlightsystem.xml

How to Create an XML Document in Silverlight


I can't seem to figure out how to create a new XML file in SilverLight. I tried using XMLTextWriter and XMLDocument but on both I get the error "does not exist in the current context"

I'm using System.Xml and I can use XMLWriter and XMLReader but I cannot for the life of me figure out what I'm doing wrong. Can someone point me in the right direction please?

Thanks!


Solution

  • You are looking for Linq to Xml and the XDocument class. Sample from MSDN:

    XDocument srcTree = new XDocument(
        new XComment("This is a comment"),
        new XElement("Root",
            new XElement("Child1", "data1"),
            new XElement("Child2", "data2"),
            new XElement("Child3", "data3"),
            new XElement("Child2", "data4"),
            new XElement("Info5", "info5"),
            new XElement("Info6", "info6"),
            new XElement("Info7", "info7"),
            new XElement("Info8", "info8")
        )
    );
    

    This is using the System.Xml.Linq namespace.

    You need to add a reference to the assembly System.Xml.Linq.dll in your Silverlight project:

    enter image description here