Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Multiline Textboxes make for happy XmlTextWriter results

This is not what I wanted to be spending the majority of my late-night coding session on tonight, but here we are: 

Tonight's lesson:  If you want to display the results of an XML file straight-up with indentations and format you are accustomed to simply and quickly, do NOT display it in a label control.  Send it instead to a multi-line Textbox.

I got sidetracked into updating an XPath test page I use in the course of coding, based on one of many good code examples in Wrox's Professional ASP.NET XML with C#.  I last wrote on this in a June post.

At bottom is the comparison between displaying the XML file in a textbox (at left in white) and a label (in pink).  I anticipate someone telling me I missed some label attribute or perhaps should have used encoding of some kind, which I would of course welcome.  Here's the code for a quick display of the XML file.

string _XmlDocPath =Server.MapPath(Request.ApplicationPath + "/xml/" + txtFilename.Text);
StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
XmlDocument doc = new XmlDocument();
doc.Load(_XmlDocPath);
doc.WriteTo(writer);
txtResults.Text = sw.ToString(); 
SubSelectLabel.Text = sw.ToString();


 

 

Posted: Sep 24 2003, 01:25 AM by daveburke | with 4 comment(s)
Filed under:

Comments

Darren Neimke said:

Dave... you just need to HtmlEncode the output like so:

Label1.Text = "<pre>" + Server.HtmlEncode( sw.ToString() ) + "</pre>
# September 24, 2003 2:30 AM

Oleg Tkachenko said:

Also it's bad idea to use XmlDocument for that - it's pure wasting of memory. Instead create XmlTextReader and give it to XmlWriter - pure streaming pipeline.
# September 24, 2003 5:40 AM

Dave Burke said:

Thank you both for your input. The <pre /> Server.HtmlEncode was exactly what I was missing, and just to get it right, I dropped XmlDocument for XmlTextReader.
# September 24, 2003 9:20 AM

Kyna said:

Hi. Beware the pull on your heartstrings - it's often the pursestrings that are actually being reached for. Help me! It has to find sites on the: Stock market day trading. I found only this - <a href="www.fabrikant-records.net/.../StockTrading">stock market trading</a>. Stock trading, the split can be gotten by financing, because the emotions of fund trade are yet run through dependent methods of aim. Stock trading, beginning resistance to a accessory population trading billion will be many for indexes finally innovative anywhere to have a next exchange credit course. With best wishes :-), Kyna from Leone.

# March 24, 2010 10:18 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)