tags:

views:

77

answers:

6

Hello All,

How to save a well formed xml string to a xml file ?

Thanks in advance...

Hi All.... I got the answer

XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml("WellFormedXMLString"); xmlDoc.Save(@"drive:\name.xml");

A: 

I am no C# programmer, but I guess you need something like this:

xmlwriter tutorial

Piero
A: 

Save the string straight onto the disk. No need to convert it into XML.

this. __curious_geek
A: 

Why do you need xml if it's just a string ? You could save a text file with the variabele name, and the string inside as variable value.

for example

MyTextVar1.txt would contain "MyTestSTring"

then you could get the var by:

var mystring = GetFileAsString( "MyTextVar1.txt" );

Run CMD
+1  A: 

You can write any string to disk like so:

File.WriteAllText(@"c:\myfile.xml", yourXmlString);

If you have a string that is not a well-formed xml string and you want to convert that to some other format, you will have to give us some example of what you want to do.

klausbyskov
A: 

the xml document is a text file itself. you only need to change its extension.

masoud ramezani
A: 

What's wrong with simply writing your string to disk?

using (StreamWriter writer = new StreamWriter(@"C:\file.xml"))
{
    writer.Write("Xml data");
    writer.Flush();
}

or if you want to "test" it:

XmlDocument doc = new XmlDocument();
try
{
    doc.LoadXml(data);
}
catch
{
    // Fix it
}
doc.Save(@"C:\file.xml");
Don