0

I've created a xml file using c#.

XmlTextWriter writer = new XmlTextWriter("Product.xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;

Then, I create my string:

string stringXML = string.Empty;
stringXML = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><configurations xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"configurations.xsd\"><ProductsList><Product><ID>1</ID><Description>EPR</Description></Product></ProductsList></configurations>";

Then I want to write my stringXML into Product.xml file.

I've tried :

System.IO.File.WriteAllText("Product.xml", stringXML);

but it doesn't worked...

How can I do this?

1

1 Answer 1

2

Try it as

string s = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><configurations xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"configurations.xsd\"><ProductsList><Product><ID>1</ID><Description>EPR</Description></Product></ProductsList></configurations>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(s);
xdoc.Save("Product.xml");

Update

string name = saveFileDialog1.FileName;
string s = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><configurations xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"configurations.xsd\"><ProductsList><Product><ID>1</ID><Description>EPR</Description></Product></ProductsList></configurations>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(s);
xdoc.Save(name);

Since saveFileDialog1 is your SaveFileDialog

Sign up to request clarification or add additional context in comments.

3 Comments

Why you are using SaveFileDialog??just give the correct path in xdoc.Save and it will save file on that location..
The user must choose the path that they want, so i need to user saveFiledialog.
I update my code according to savefiledialog ..please check it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.