Formatting raw XML?

  • Thread starter Thread starter clintonG
  • Start date Start date
clintonG said:
I'm using the XmlTextWriter but see no way to format raw XML like this:

<rss version="2.0" xmlns:cf
="http://www.microsoft.com/schemas/rss/core/2005"
xmlns:media="http://search.yahoo.com/mrss"
xmlns:dc="http://purl.org/dc/elements/1.1/">

Is it possible to format the raw XML to make it more readable?

Define "more readable". XML is a file format primarily designed for
interchanging data between computers, and to be *somewhat* readable to
humans, but only as a secondary concern. A lot of XML just isn't going to
look pretty.
What about some other way to write the XML to the filesystem so it is
readable in this context?
I posted to microsoft.public.xml but it isn't very active.

You have the option to turn on indentation with the .Indentation property.
If you're using .NET 2.0, the recommended way of creating XML writers will
also allow you to include a setting for outputting newlines after every
attribute:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
using (XmlWriter writer = XmlWriter.Create(..., settings)) {
...
}

That's about as far as pretty-printing goes, however. If you need to have it
even *more* readable, you might want to look at writing an XSL stylesheet.
This will allow you to view the XML as anything, including pretty HTML.

In theory you could also write your own XmlWriter that does pretty-printing
the way you want it. This is far more trouble than it's worth, though.
 
<snip />

I've maxed out the use of the .Indentation method and I can't and won't even
try to argue your sage advice about other methods such as transformation. I
suppose I'll cope with all of the namespaces displayed on a single line
compelling a horizontal scroll when wanting to read the raw XML. Thanks for
your comments...
 
clintonG said:
I've maxed out the use of the .Indentation method and I can't and won't
even try to argue your sage advice about other methods such as
transformation. I suppose I'll cope with all of the namespaces displayed
on a single line compelling a horizontal scroll when wanting to read the
raw XML. Thanks for your comments...

Well, this may sound stupid, but if you simply open the XML file in Internet
Explorer it will display it without the need for horizontal scrolling. Of
course, if your XML file is very large IE will probably fail miserably.

You could also simply use a text editor with word wrapping and syntax
highlighting. My favorite's TextPad.
 
Back
Top