473,325 Members | 2,792 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,325 software developers and data experts.

Bad XML formatting from XmlTextWriter

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);
System.Xml.XmlDocument d = new System.Xml.XmlDocument();

try
{
//load the xml into the document object
d.LoadXml(sXML);

w.Formatting = System.Xml.Formatting.Indented;

//copy the xml into a formatting XmlTextWriter
d.WriteContentTo(w);
w.Flush();
ms.Flush();

//rewind the memory stream before reading from it
ms.Position = 0;

//extract the formatted text
result = new System.IO.StreamReader(ms).ReadToEnd();
}
catch (System.Xml.XmlException)
{
}
finally
{
try {ms.Close();} catch (Exception) {}
try {w.Close();} catch (Exception) {}
}

return result;
}

It works pretty well, most of the time. Given this string:

"<root><childNode1><deepestNode>I am the deepest
node!</deepestNode></childNode1><childNode2><deepestNode>I am also
quite deep!</deepestNode></childNode2></root>"

It produces this nicely formatted output:

<root>
<childNode1>
<deepestNode>I am the deepest node!</deepestNode>
</childNode1>
<childNode2>
<deepestNode>I am also quite deep!</deepestNode>
</childNode2>
</root>

But when a child node has a value and also has a child node of its
own, as childNode1 does here:

"<root><childNode1>I have a value<deepestNode>I am the deepest
node!</deepestNode></childNode1><childNode2><deepestNode>I am also
quite deep!</deepestNode></childNode2></root>"

This is the unfortunate result (deepestNode is inline with
childNode1):

<root>
<childNode1>I have a value<deepestNode>I am the deepest
node!</deepestNode></childNode1>
<childNode2>
<deepestNode>I am also quite deep!</deepestNode>
</childNode2>
</root>

I had expected this:

<root>
<childNode1>I have a value
<deepestNode>I am the deepest node!</deepestNode>
</childNode1>
<childNode2>
<deepestNode>I am also quite deep!</deepestNode>
</childNode2>
</root>

Is the unexpected inlining of childNode1's child a bug in the
XmlTextWriter? Is there a fix or workaround?
Nov 12 '05 #1
2 2016
vector wrote:
But when a child node has a value and also has a child node of its
own, as childNode1 does here:

"<root><childNode1>I have a value<deepestNode>I am the deepest
node!</deepestNode></childNode1><childNode2><deepestNode>I am also
quite deep!</deepestNode></childNode2></root>"

This is the unfortunate result (deepestNode is inline with
childNode1):
This is by design. In mixed context (text and elements mixed) indenting
is ignored, because that's impossible to format such data without adding
significant whitespace characters thus changing document's data.

I had expected this:

<root>
<childNode1>I have a value
<deepestNode>I am the deepest node!</deepestNode>


Here you have modified the first text node from "I have a value" to "I
have a value\n\t\t". It's not allowed for formatters/pretty-printers to
modify significant parts of a document. Adding whitespace between tags
is safe - that's insignificant whitespace, but adding whitespace to text
means changing that text.
--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
vector wrote:
But when a child node has a value and also has a child node of its
own, as childNode1 does here:

"<root><childNode1>I have a value<deepestNode>I am the deepest
node!</deepestNode></childNode1><childNode2><deepestNode>I am also
quite deep!</deepestNode></childNode2></root>"

This is the unfortunate result (deepestNode is inline with
childNode1):
This is by design. In mixed context (text and elements mixed) indenting
is ignored, because that's impossible to format such data without adding
significant whitespace characters thus changing document's data.

I had expected this:

<root>
<childNode1>I have a value
<deepestNode>I am the deepest node!</deepestNode>


Here you have modified the first text node from "I have a value" to "I
have a value\n\t\t". It's not allowed for formatters/pretty-printers to
modify significant parts of a document. Adding whitespace between tags
is safe - that's insignificant whitespace, but adding whitespace to text
means changing that text.
--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #3

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

Similar topics

5
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...
6
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"...
0
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...
1
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...
4
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...
2
by: Ken Wilson | last post by:
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); ...
3
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...
0
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>...
1
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.