473,396 Members | 1,895 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,396 software developers and data experts.

Can I make this document format correctly?

I need to build a document by adding sub docs to it. When I do so, I
lose formatting. I've tried a suggestion I saw of simply setting the
formatting a second time to no avail.

Following is a console snippet that demonstrates the problem:

static void Main( string[] args )
{
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter( sw );
xw.Formatting = Formatting.Indented;

StringWriter sw2 = new StringWriter();
XmlTextWriter xw2 = new XmlTextWriter( sw2 );
xw2.Formatting = Formatting.Indented;

xw.WriteStartDocument();
xw.WriteStartElement( "Parent" );
xw.WriteStartElement( "ParentInner" );
xw.WriteString( "hi mom" );
xw.WriteEndElement();

xw2.WriteStartDocument();
xw2.WriteStartElement( "Child" );
xw2.WriteStartElement( "ChildInner" );
xw2.WriteString( "hi again" );
xw2.WriteEndElement();
xw2.WriteEndElement();
xw2.WriteEndDocument();

xw.WriteRaw( sw2.GetStringBuilder().ToString() );
xw.WriteEndElement();
xw.WriteEndDocument();

using(StreamWriter streamW = new StreamWriter(@"d:\test.xml", false ))
{
streamW.Write( sw.GetStringBuilder().ToString() );
}

xw.Close();
xw2.Close();
}
Nov 15 '05 #1
4 1395
Hi Brad,

The XmlTextWriter maintains several fields to keep track of identation
level and whether a new line is needed. Everything works well as long
as you call only methods like WriteStartElement and WriteEndElement.
However, when you call WriteRaw, the writer does not attempt to parse
your raw data to see if it is a well-formed XML fragment. It just
treats the raw data as its name suggests: "raw data". The format
including white space, new lines, etc. of the raw data is preserved,
but it does not fit well in the existing element.

Thi

Nov 16 '05 #2
This I know. Is there a way to format my result correctly?

"Truong Hong Thi" wrote:
The XmlTextWriter maintains several fields to keep track of identation
level and whether a new line is needed. Everything works well as long
as you call only methods like WriteStartElement and WriteEndElement.
However, when you call WriteRaw, the writer does not attempt to parse
your raw data to see if it is a well-formed XML fragment. It just
treats the raw data as its name suggests: "raw data". The format
including white space, new lines, etc. of the raw data is preserved,
but it does not fit well in the existing element.


Nov 16 '05 #3
>This I know. Is there a way to format my result correctly?
OK. I would like to tell a solution that works.

First thing, you need to remove the following lines:
xw2.WriteStartDocument();
xw2.WriteEndDocument();
Those statements create a second document declaration in the combined
xml and make it ill-formed. The file "d:\test.xml" generated by the
snippet you posted should be malformed because of that.

Then, as I said, you should try to write to "xw" by using methods other
than WriteRaw. Remove the following line:
xw2.Formatting = Formatting.Indented;
That line confuses the writer when combining.

Add the following lines to replace for WriteRaw:
XmlReader r = new XmlTextReader(new
StringReader(sw2.GetStringBuilder().ToString()));
xw.WriteNode(r, false);

Then everything should be OK. You can test it by writing on screen
instead of to a file:
Console.WriteLine( sw.GetStringBuilder().ToString() );

Hope it helps,
Thi

Nov 16 '05 #4
Thanks!

Truong Hong Thi wrote:
First thing, you need to remove the following lines:
xw2.WriteStartDocument();
xw2.WriteEndDocument();
Those statements create a second document declaration in the combined
xml and make it ill-formed. The file "d:\test.xml" generated by the
snippet you posted should be malformed because of that.

Then, as I said, you should try to write to "xw" by using methods other
than WriteRaw. Remove the following line:
xw2.Formatting = Formatting.Indented;
That line confuses the writer when combining.

Add the following lines to replace for WriteRaw:
XmlReader r = new XmlTextReader(new
StringReader(sw2.GetStringBuilder().ToString()));
xw.WriteNode(r, false);

Nov 18 '05 #5

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

Similar topics

11
by: Ken | last post by:
How do I force the text generated by a document.write command to wrap after a specified number of characters? Thanks for the help.
14
by: keith edward | last post by:
Hello, I have a long web document... about 30 pages. It is currently in HTML (tables)... how do I format it so that is will print the pages correctly? Thanks, Keith
3
by: Jason James | last post by:
Hi all, I have an application that needs to print numerous documents of different styles (either to the printer or print preview dialog). I have selected which document type to print by...
17
by: JellyON | last post by:
Hi. How to do document.lastModified works with Netscape and Opera ? Or what other way to take to simply display the last update date about current page ?
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
8
by: SL | last post by:
I try to validate against a schema a document stored in several files thanks to external entities. The parseur add a 'xml:base="url"' attribute on the root element of this sub-trees during parsing,...
2
by: windsorben | last post by:
I'd like to have an image appear after the student answers each short answer question correctly. I can't seem to get it to work properly. See code below. Thanks! <html> <head>...
5
by: jhurrell | last post by:
I have been having some trouble getting my XSL style sheet to parse correctly. I have some XML outputted from an SQL-Server, that I then need to turn into multiple HTML files. This I have done...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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...
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
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...
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.