Tejas Sutar
Tejas Sutar

Reputation: 777

Create XML document at given path

It's very clear from the title that I want to create a XML document in my Silverlight 5 app at certain path. I know we can use XDocument available in System.Xml.Linq to create XML file. As I understand, the file is created in "My Documents" folder if we use XDocument. How can I save XML document to desired path? Is it possible to save XML document to desired path with elevated trust in Silverlight 5 without "Save File Dialog"?

UPDATE: Though there is a method Save() available in XDocument there is no overload for Save() in Silverlight 5 which accepts string parameter otherwise it would be easy to send path to Save(). I tried to save using StreamWriter. Below is my code:

using (var outfile = new StreamWriter(@"C:\Users\Tejas\Desktop\Sample.xml"))
{
       var doc = new XDocument(new XElement("Root", new XElement("Child", "content")));
       doc.Save(outfile);
}

But it's giving me System.Security.SecurityException with the message File operation not permitted. Access to path 'C:\Users\Tejas\Desktop\Sample.xml' is denied. Is it because my Silverlight 5 application is not trusted? One more thing I want to mention that I don't want to achieve it using "SaveFileDialog".

Upvotes: 2

Views: 6866

Answers (1)

Jeepx
Jeepx

Reputation: 51

You create your XDocument as you wish, fill it up with the XML-structure you like. And at the end you can call the Save method on the XDocument to save it to a location.

A very simple Example, lets say you want to save to the location 'c:\myXMLFolder\newXMLfiles\file.xml':

XDocument doc = new XDocument(
    new XElement("Root",
        new XElement("Child", "content")
    )
);
doc.Save("c:\\myXMLFolder\\newXMLfiles\\file.xml");

Upvotes: 2

Related Questions