471,603 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,603 software developers and data experts.

Formatted Xml

I have a method in my class that needs to return formatted XML, with the
carriage returns, linefeeds, and tabs... However, when I return
oXml.OuterXml, the Xml is not formatted...

Every example I've seen involves writing the Xml to disk before it's
formatted (or perhaps is formatted while it's being written) which doesn't
help me at all...

So how can I return a formatted Xml string from the XmlDocument object?

Thanx!
Dec 6 '05 #1
2 2365


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/
Dec 6 '05 #2
Thanx... that was exactly what I needed...

On Tue, 06 Dec 2005 13:01:14 -0700, Martin Honnen <ma*******@yahoo.de>
wrote:


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.


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Dec 6 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Paul Lamonby | last post: by
4 posts views Thread by The Roys | last post: by
8 posts views Thread by Matthew Thorley | last post: by
3 posts views Thread by Niel | last post: by
2 posts views Thread by Steven T. Hatton | last post: by
3 posts views Thread by Craig Petrie | last post: by
4 posts views Thread by cybervigilante | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.