30

I have this string variable:

string xml = @"<Contacts> 
    <Contact> 
    <Name>Patrick Hines</Name> 
    <Phone Type=""Home"">206-555-0144</Phone> 
    <Phone Type=""Work"">425-555-0145</Phone> 
    <Phone Type=""Mobile"">332-899-5678</Phone> 
    <Address> 
        <Street1>123 Main St</Street1> 
        <City>Mercer Island</City> 
        <State>WA</State> 
        <Postal>68042</Postal> 
    </Address> 
    </Contact> 
    <Contact> 
    <Name>Dorothy Lee</Name> 
    <Phone Type=""Home"">910-555-1212</Phone> 
    <Phone Type=""Work"">336-555-0123</Phone> 
    <Phone Type=""Mobile"">336-555-0005</Phone> 
    <Address> 
        <Street1>16 Friar Duck Ln</Street1> 
        <City>Greensboro</City> 
        <State>NC</State> 
        <Postal>27410</Postal> 
    </Address> 
    </Contact>
</Contacts>";

How can I save this string into an XML file in my drive c? Using c#.

1
  • Use System.IO.File class methods. Commented Jan 18, 2011 at 8:06

6 Answers 6

70

The fact that it's XML is basically irrelevant. You can save any text to a file very simply with File.WriteAllText:

File.WriteAllText("foo.xml", xml);

Note that you can also specify the encoding, which defaults to UTF-8. So for example, if you want to write a file in plain ASCII:

File.WriteAllText("foo.xml", xml, Encoding.ASCII);
Sign up to request clarification or add additional context in comments.

15 Comments

what if sir if i want to read my saved xml back into my program as a string? Should I use XmlTextReader?
@yonan2236: Then just use File.ReadAllText. As Jon Skeet says, you don't really appear to be doing anything XML-specific; you only want to do basic file I/O on text data?
@yonan2236: It's not that the question is wrong as such - it's more that it's mixing two concepts: saving a text file, and the contents of the text file being XML.
I'd also recommend adding an XML declaration to the string, and ensuring the encoding in the declaration matches that in the WriteAllText method. It's likely to save a fair amount of hassle at later date, judging by the frequency of XML encoding questions on stackoverflow.
@stt106: Well that just means that your string contains the text encoding="utf-16". That wouldn't be changed by any encoding specified in File.WriteAllText - it's just part of the string. The file will still be encoded in UTF-8, meaning that the XML declaration is effectively lying.
|
13
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(yourXMLString);
xdoc.Save("myfilename.xml");

Comments

8

If you don't need to do any processing on the string (with an XML library, for example), you could just do:

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

1 Comment

+1 For using correct path, should save some head-scratching for the OP. :)
6
System.IO.File.WriteAllText("filename.xml", xml );

Comments

3

You can do this:

string path = @"C:\testfolder\testfile.txt";

using (System.IO.StreamWriter file = new System.IO.StreamWriter(path))
{    
    file.Write(text);  
}

You can also do this after you've created an XML Document, but it is slower:

xdoc.Save

Comments

2

If you want to save the string as-is without performing any check on whether it's well-formed or valid, then as has been answered above, use System.IO.File.WriteAllText("C:\myfilename.xml", xml );

As has also been noted, this defaults to saving the file as UTF-8, but you can specify encoding as Jon Skeet mentioned.

I'd recommend adding an XML declaration to the string, e.g.,

<?xml version="1.0" encoding="UTF-8"?>

and ensuring the encoding in the declaration matches that in the WriteAllText method. It's likely to save a fair amount of hassle at later date, judging by the frequency of XML encoding questions on stackoverflow.

If you want to ensure the XML is well-formed and/or valid, then you will need to use an XML parser on it first, such as XDocument doc = XDocument.Parse(str); That method is also overridden if you want to preserve whitespace: XDocument.Parse(str, LoadOptions.PreserveWhitespace)

You can then perform validation on it http://msdn.microsoft.com/en-us/library/bb340331.aspx

before saving to file: doc.Save("C:\myfilename.xml");

Comments

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.