Connecting Tech Pros Worldwide Forums | Help | Site Map

Formatting XML Output From XmlTextWriter

Ken Wilson
Guest
 
Posts: n/a
#1: Nov 17 '05
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(filename);

tw.Formatting = Formatting.Indented;
tw.WriteStartDocument();
tw.WriteStartElement("MyRoot");
tw.WriteStartElement("MyString);
tw.WriteString("This is the string");
tw.WriteEndElement();
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();

The output I get is formatted as follows.

<?xml version="1.0"?>
<MyRoot>
<MyString>This 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

Martin Honnen
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Formatting XML Output From XmlTextWriter




Ken Wilson wrote:
[color=blue]
> I am writing and .xml file and am not getting the formatting I would
> like.[/color]
[color=blue]
> XmlTextWriter tw = new XmlTextWriter(filename);
>
> tw.Formatting = Formatting.Indented;[/color]

[color=blue]
> tw.WriteStartElement("MyString);
> tw.WriteString("This is the string");
> tw.WriteEndElement();[/color]
[color=blue]
> The output I get is formatted as follows.[/color]
[color=blue]
> <MyString>This is the string</MyString>[/color]
[color=blue]
> 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>[/color]

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/
Ken Wilson
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Formatting XML Output From XmlTextWriter


On Thu, 03 Nov 2005 20:33:08 +0100, Martin Honnen <mahotrash@yahoo.de>
wrote:
[color=blue]
>
>
>Ken Wilson wrote:
>[color=green]
>> I am writing and .xml file and am not getting the formatting I would
>> like.[/color]
>[color=green]
>> XmlTextWriter tw = new XmlTextWriter(filename);
>>
>> tw.Formatting = Formatting.Indented;[/color]
>
>[color=green]
>> tw.WriteStartElement("MyString);
>> tw.WriteString("This is the string");
>> tw.WriteEndElement();[/color]
>[color=green]
>> The output I get is formatted as follows.[/color]
>[color=green]
>> <MyString>This is the string</MyString>[/color]
>[color=green]
>> 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>[/color]
>
>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.[/color]

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
Closed Thread