Link to home
Start Free TrialLog in
Avatar of chuang4630
chuang4630

asked on

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?
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
Avatar of chuang4630
chuang4630

ASKER

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
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();
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What is ws in XmlTextWriter.Create(sb, ws);?
as mentioned in my previous post:  the writer settings