How do I write the xml to a string?

I need to create a XML file on the fly. The current code only writes the xml to the file as follows:

XmlTextWriter writer = new XmlTextWriter(xmlMyRequestFile, Encoding.UTF8);

How do I write the xml to a string?
Who is Participating?
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
You will need previously:
                    XmlWriterSettings ws = new XmlWriterSettings();
So you can change some serialization settings

Geert BormansInformation ArchitectCommented:
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
chuang4630Author Commented:
The compiler gives me the error:

Error      4      The best overloaded method match for 'System.Xml.XmlTextWriter.XmlTextWriter(System.IO.Stream, System.Text.Encoding)' has some invalid arguments      C:\Documents and Settings\Chris Huang\My Documents\Visual Studio 2005\Projects\Hilton.CCS.Business\OrderShipFromATT.cs      135      36      Hilton.CCS.Business
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Jaime OlivaresSoftware ArchitectCommented:
Try with something like:

                    StringBuilder sb = new StringBuilder();
                    XmlWriter xw = XmlTextWriter.Create(sb, ws);
                    // some xml writting
                    // xw.WriteStartElement(...);
                    // xw.WriteEndElement();

                    string s = sb.ToString();
chuang4630Author Commented:
What is ws in XmlTextWriter.Create(sb, ws);?
Jaime OlivaresSoftware ArchitectCommented:
as mentioned in my previous post:  the writer settings
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.