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?
LVL 1
chuang4630Asked:
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

0
 
Geert BormansInformation ArchitectCommented:
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
0
 
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
0
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();

                    xw.Close();
                    string s = sb.ToString();
0
 
chuang4630Author Commented:
What is ws in XmlTextWriter.Create(sb, ws);?
0
 
Jaime OlivaresSoftware ArchitectCommented:
as mentioned in my previous post:  the writer settings
0
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.