Amit
Amit

Reputation: 3478

convert string to a xml file?

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");

Upvotes: 0

Views: 6662

Answers (6)

masoud ramezani
masoud ramezani

Reputation: 22950

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

Upvotes: 0

Don
Don

Reputation: 9661

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");

Upvotes: 1

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 121037

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.

Upvotes: 1

Run CMD
Run CMD

Reputation: 3035

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" );

Upvotes: 0

this. __curious_geek
this. __curious_geek

Reputation: 43217

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

Upvotes: 0

Pierre
Pierre

Reputation: 5156

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

xmlwriter tutorial

Upvotes: 0

Related Questions