Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output a XmlDocument to a string

Status
Not open for further replies.
Jun 9, 2006
159
US


I've created an XmlDocument object and would like to send the output to a string. The save method of the document seenms like it only acepts and XmlWriter. How do I adapt that to write to a string instead of a file?

My ultimate goal is to send the XML to a flash player via a querystring.

XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);

// songs element
XmlNode songs = doc.CreateElement("songs");
doc.AppendChild(songs);

// here i want to save to a string and send to querystring.
 
Load the XmlDoc data into a stream, convert the stream into a byte array, then sstuff it into a string like this:

string xml = System.Text.Encoding.UTF8.GetString(yourByteArray);

 
Thanks for your response.

The xmlwriter that the XmlDocument.Save method accepts seems to be an abstract class. How am I able to use the stream to populate the array?

-- shawn
 
Hi,

I may be misunderstanding your requirements but this might help to get a string representation of the contents of an xml doc.

XmlDocument docXML = new XmlDataDocument();
//here you could make your xml doc
docXML.Load( Server.MapPath( "MyXML.xml" ) );

XmlElement nodRoot = docXML.DocumentElement;

string allMyChildren = nodRoot.InnerText;

The contents of the xml are in the allMyChildren variable.

Hope this helps a little

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top