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.CreateElement("gods");
XmlElement god = xmlDocument.CreateElement("god");
god.AppendChild(xmlDocument.CreateTextNode("Kibo") );
gods.AppendChild(god);
xmlDocument.AppendChild(gods);
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = Formatting.Indented;
xmlDocument.WriteTo(xmlWriter);
xmlWriter.Flush();
xmlWriter.Close();
string formattedXML = stringWriter.ToString();
Console.WriteLine("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/