473,609 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatting XML Output From XmlTextWriter

I am writing and .xml file and am not getting the formatting I would
like. The portion of the code that is giving me problems is as
follows;

XmlTextWriter tw = new XmlTextWriter(f ilename);

tw.Formatting = Formatting.Inde nted;
tw.WriteStartDo cument();
tw.WriteStartEl ement("MyRoot") ;
tw.WriteStartEl ement("MyString );
tw.WriteString( "This is the string");
tw.WriteEndElem ent();
tw.WriteEndElem ent();
tw.WriteEndDocu ment();
tw.Flush();
tw.Close();

The output I get is formatted as follows.

<?xml version="1.0"?>
<MyRoot>
<MyString>Thi s is the string</MyString>
</MyRoot>

My questions is how do I get the MyString tags and the string itself
to each appear on consecutive lines, i.e.

<MyString>
This is the string
</MyString>

Ken Wilson
Seeking viable employment in Victoria, BC
Nov 17 '05 #1
2 6315


Ken Wilson wrote:
I am writing and .xml file and am not getting the formatting I would
like. XmlTextWriter tw = new XmlTextWriter(f ilename);

tw.Formatting = Formatting.Inde nted;
tw.WriteStartEl ement("MyString );
tw.WriteString( "This is the string");
tw.WriteEndElem ent(); The output I get is formatted as follows. <MyString>Thi s is the string</MyString> My questions is how do I get the MyString tags and the string itself
to each appear on consecutive lines, i.e.

<MyString>
This is the string
</MyString>


There is nothing in the XmlTextWriter implementation that gives you that
formatting, it does not make much sense to have white space added
automatically to text content in an element.
If you want that you will need your own implementation of XmlWriter or
at least extend XmlTextWriter.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 17 '05 #2
On Thu, 03 Nov 2005 20:33:08 +0100, Martin Honnen <ma*******@yaho o.de>
wrote:


Ken Wilson wrote:
I am writing and .xml file and am not getting the formatting I would
like.

XmlTextWriter tw = new XmlTextWriter(f ilename);

tw.Formatting = Formatting.Inde nted;


tw.WriteStartEl ement("MyString );
tw.WriteString( "This is the string");
tw.WriteEndElem ent();

The output I get is formatted as follows.

<MyString>Thi s is the string</MyString>

My questions is how do I get the MyString tags and the string itself
to each appear on consecutive lines, i.e.

<MyString>
This is the string
</MyString>


There is nothing in the XmlTextWriter implementation that gives you that
formatting, it does not make much sense to have white space added
automaticall y to text content in an element.
If you want that you will need your own implementation of XmlWriter or
at least extend XmlTextWriter.


Thank you for your reply. You confirm what I suspected. At this time
I will take the stance that if someone wants it 'pretty' printed they
can format it however they wish when they parse it. Once again, your
reply is appreciated.

Ken Wilson
Seeking viable employment in Victoria, BC
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2160
by: cnu | last post by:
Hi, I have to make a formatted (indented) xml file like this : <?xml version="1.0"?> <Message> <Parent index="0" length="6">123 <Child1 index="0,1" length="6">abcd</Child1> <Child2 index="0,2" length="6">pqrsxyz</Child2> </Parent> </Message>
6
4834
by: Ot | last post by:
I have an xml document similar to this <all> <one> <options attr1="1" attr2="2" /> <item name="a name" attr1="1" /> <item name="another" attr1="2"/> </one> <two> <options attr1="1" attr2="2" />
2
2518
by: Miki Szinegh | last post by:
Hello All, I have an asp.net search application that brings up a datagrid of results, with one of the fields being a hyperlink to an XML document, of which there are thousands. By clicking this link, the browser loads the XML document in its raw form. My question is, how do I use some of these formatting techniques (XSL etc) to format this XML the way I want to, after the link is clicked on? A nudge in the right direction would be...
2
2056
by: vector | last post by:
Here is a function more or less exactly as I found it from somewhere on the internet. static string BeautifyXML(string sXML) { string result = ""; System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(ms, System.Text.Encoding.Unicode);
1
10178
by: Riko Eksteen | last post by:
Hi I'm reading an xml file into an XmlDocument, adding some nodes, and writing it back out. I would like the nodes I add to assume the same level of indeting as the rest of the document. (I load the document with PreserveWhiteSpace = true.) I thought I could do this by using an XmlTextWriter with Formatting set to Formatting.Indented, but this doesn't work. The Formatting.Indented on the XmlTextWriter only indents my output when I...
4
1248
by: Adrian Parker | last post by:
I have an xml document object with xml in it, how can I convert the xml to a string and format it so that it is is indented correctly. I've looked at XMLTextWriter and the Format property, but am confused as to how to get the data to a string and not to an output stream.. Also, if I have to write it to a file first, it seems to either convert all the < to their html quoted formats or encodes it using UTF7/8 etc. Only way I can get it...
3
1158
by: Urs Vogel | last post by:
Hi When using the XmlDocument.OuterXml property after adding some nodes and attributes, it returns the entire document correctly, but it's actually all on one line. How can I format the document that it becomes readable by humans, with CRLFs and Tabs? Thanks for any hints. Urs
0
1224
by: marfi95 | last post by:
I have an xml file that I want to maintain the whitespace on and spacing between my sections. I have something like this. <!-- Comment lines A --> <!-- Comment lines A --> <A> <B>test</B> <B>test1</B> <B>test2</B> </A>
1
3103
by: JAM | last post by:
I'm trying to code some directory structure as my output / input file using XML. I would like to see formatiing with indentations. mimicking directory tree structure. Unfortunately the code such as this: using (XmlTextWriter xw = new XmlTextWriter("C:\\Temp\\test.xml", Encoding.UTF8)) { xw.Formatting = Formatting.Indented; xw.WriteStartDocument(); xw.WriteStartElement("FILTER", "");
0
8126
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8065
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
8563
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8210
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
8394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6990
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...
0
5507
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();...
1
2527
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
0
1379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.