JSheble wrote:
So how can I return a formatted Xml string from the XmlDocument object?
You can use the WriteTo method to write to an XmlTextWriter over a
StringWriter where you set the XmlTextWriter to format as you need it e.g.
XmlDocument xmlDocument = new XmlDocument();
XmlElement gods = xmlDocument.Cre ateElement("god s");
XmlElement god = xmlDocument.Cre ateElement("god ");
god.AppendChild (xmlDocument.Cr eateTextNode("K ibo"));
gods.AppendChil d(god);
xmlDocument.App endChild(gods);
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(s tringWriter);
xmlWriter.Forma tting = Formatting.Inde nted;
xmlDocument.Wri teTo(xmlWriter) ;
xmlWriter.Flush ();
xmlWriter.Close ();
string formattedXML = stringWriter.To String();
Console.WriteLi ne("Formatted XML:\r\n{0}", formattedXML);
gives
<gods>
<god>Kibo</god>
</gods>
You could also use the Save method to Save to a MemoryStream or to an
XmlTextWriter over a StringWriter or a StringWriter itself.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/