473,410 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 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 2424


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: James Turner | last post by:
I am trying to store formatted text (windows format) into a MySQL database and then retrieve it. The field in the database is a varchar. I cut and paste the test into a form field formatted, then...
5
by: Paul Lamonby | last post by:
Hi, I am generating a HTML formatted email in PHP, but I am getting the most bizarre error, it will randomly delete characters out of the HTML code while in transit to the address, therefore...
4
by: The Roys | last post by:
Hi I have a textbox which displays a formatted numeric. eg: 100,000 When I retreive this value for calculations, it is valued at 100 eg: calc = val(txtbox) * 2 calc will equal 200, not...
8
by: Matthew Thorley | last post by:
Greetings, perhaps someone can explain this. I get to different styles of formatting for xmla and xmlb when I do the following: from elementtree import ElementTree as et xmla =...
3
by: Niel | last post by:
Hello friends, Don't know if i am posting to the right group, but if anyone has an idea about this please let me know. I just wanted to ask that is it true that it is not possible to send HTML...
2
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent:...
3
by: Craig Petrie | last post by:
Hi, I have a large table in Word 2003 that has formatted text in the cells and wish to read and convert a cells formatted contents to html output via vb.net code. The formatting contains the...
4
by: cybervigilante | last post by:
I sent HTML formatted email, using PHP, to my Yahoo address from my server, and it came out fine, styles and all. I sent it to my gmail address to test it and all I see is the raw html code. But I...
1
by: EricBlair | last post by:
Hello, Wondering if someone might help on this... I write out formatted text to a file then load it into a RichTextBox. However the text that is formatted into a table displays like this...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.