473,664 Members | 3,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2442


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

On Tue, 06 Dec 2005 13:01:14 -0700, Martin Honnen <ma*******@yaho o.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.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.


--
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
17705
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 call the PHP program to add the record using POST. When I get the text out, it has lost it's formatting. Formatting in the text is achieved by multiple spaces not tabs, but even multiple spaces are converted to a single space and the carrage...
5
2243
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 generating errors in the final output. For instance, a "</td>" tag will become "/td>" or a URL will end in ".co.k", missing the "u". This seems to happen totally randomly from one try to the next throughout the code.
4
3031
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 200,000. Why is this, how do I fix it ??
8
5213
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 = et.ElementTree('some_file.xml') xmlb = et.Element('parent') et.SubElement(xmlb, 'child1') et.SubElement(xmlb, 'child2')
3
4719
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 Formatted emails from ASP through CDO. (CDOSYS). I mean I am able to send the email which is HTML formatted through CDONTS that sends email through Virtual SMTP server and i get a proper looking email. But when i tried same in CDO then all i got...
2
3500
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: http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#1 Copying a file: WRONG WAY: #include <fstream> std::ifstream IN ("input_file"); std::ofstream OUT ("output_file");
3
3362
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 following: bold text, italic text, superscript & subscript text, bullet points. The output html is then saved in an XML file as a CDATA section for later use in the application when formatted output is required. I have looked up many sites on the...
4
5059
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 do get formatted email in gmail, so I know people make it work somehow. I'm looking right at a gmail message that I copied to Dreamweaver and it's using Strong and Red text attributes that work in gmail. But mine don't. What do you do...
1
1393
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 below, all jagged. When I view the actual file, it is formatted properly and when I copy the text from the RT Box it is also formatted, it just displays wrong. Any ideas?? Is there3 a setting I'm missing? Header1 Header2 Header3 ABCD ...
0
8348
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7375
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.