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

Pretty print XML?

I am sure this has been asked enough to warrant an FAQ but I could not find
it.

Does anyone have some code and maybe an XSLT stylesheet that would allow me
to transform an XML string to a "pretty" version (nodes indented on separate
lines, etc.)?

Thank you for your help.

Kevin
Nov 12 '05 #1
5 15714
Kevin Burton wrote:
I am sure this has been asked enough to warrant an FAQ but I could not
find it.

Does anyone have some code and maybe an XSLT stylesheet that would allow
me to transform an XML string to a "pretty" version (nodes indented on
separate lines, etc.)?


Open it in IE or Firefox.
Or use an XML editor that has an indent-and-reformat function.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Nov 12 '05 #2

I want to put the "pretty" XML in an .NET RTF Text box. Is there an XSLT
transform or some .NET Framework function that I can call? In VB there is the
"Indent" function with the MSXML toolkit, but I am using C#. I would like a
more direct approach. Spawning IE and trying to get the text out would be too
cumbersome. I think that an XSLT transform will be more direct. I just don't
have the time right now to write it. Since this is used so frequently I was
hoping that there might already be a Framework function to do this.

Kevin

"Peter IEFlynn" wrote:
Kevin Burton wrote:
I am sure this has been asked enough to warrant an FAQ but I could not
find it.

Does anyone have some code and maybe an XSLT stylesheet that would allow
me to transform an XML string to a "pretty" version (nodes indented on
separate lines, etc.)?


Open it in IE or Firefox.
Or use an XML editor that has an indent-and-reformat function.

///Peter
--
XML FAQ: http://xml.silmaril.ie/

Nov 12 '05 #3
I think this is what you want. Probably a more efficient way to do it...

using System;

using System.IO;

using System.Xml;

namespace XmlTest

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

using (StringWriter stringWriter = new StringWriter())

{

XmlDocument doc = new XmlDocument();

//get your document

doc.LoadXml("<root><element foo=\"bar\"><child></child></element></root>");

//create reader and writer

XmlNodeReader xmlReader = new XmlNodeReader(doc);

XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);

//set formatting options

xmlWriter.Formatting = Formatting.Indented;

xmlWriter.Indentation = 1;

xmlWriter.IndentChar = '\t';

//write the document formatted

xmlWriter.WriteNode(xmlReader, true);

Console.WriteLine(stringWriter.ToString());

}
Console.ReadLine();

}

}

}

Cheers,

Kent

"Kevin Burton" <Ke*********@discussions.microsoft.com> wrote in message
news:62**********************************@microsof t.com...

I want to put the "pretty" XML in an .NET RTF Text box. Is there an XSLT
transform or some .NET Framework function that I can call? In VB there is
the
"Indent" function with the MSXML toolkit, but I am using C#. I would like
a
more direct approach. Spawning IE and trying to get the text out would be
too
cumbersome. I think that an XSLT transform will be more direct. I just
don't
have the time right now to write it. Since this is used so frequently I
was
hoping that there might already be a Framework function to do this.

Kevin

"Peter IEFlynn" wrote:
Kevin Burton wrote:
> I am sure this has been asked enough to warrant an FAQ but I could not
> find it.
>
> Does anyone have some code and maybe an XSLT stylesheet that would
> allow
> me to transform an XML string to a "pretty" version (nodes indented on
> separate lines, etc.)?


Open it in IE or Firefox.
Or use an XML editor that has an indent-and-reformat function.

///Peter
--
XML FAQ: http://xml.silmaril.ie/

Nov 12 '05 #4
This was precisly what I was looking for. Thank you.

"Kent Boogaart" wrote:
I think this is what you want. Probably a more efficient way to do it...

using System;

using System.IO;

using System.Xml;

namespace XmlTest

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

using (StringWriter stringWriter = new StringWriter())

{

XmlDocument doc = new XmlDocument();

//get your document

doc.LoadXml("<root><element foo=\"bar\"><child></child></element></root>");

//create reader and writer

XmlNodeReader xmlReader = new XmlNodeReader(doc);

XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);

//set formatting options

xmlWriter.Formatting = Formatting.Indented;

xmlWriter.Indentation = 1;

xmlWriter.IndentChar = '\t';

//write the document formatted

xmlWriter.WriteNode(xmlReader, true);

Console.WriteLine(stringWriter.ToString());

}
Console.ReadLine();

}

}

}

Cheers,

Kent

"Kevin Burton" <Ke*********@discussions.microsoft.com> wrote in message
news:62**********************************@microsof t.com...

I want to put the "pretty" XML in an .NET RTF Text box. Is there an XSLT
transform or some .NET Framework function that I can call? In VB there is
the
"Indent" function with the MSXML toolkit, but I am using C#. I would like
a
more direct approach. Spawning IE and trying to get the text out would be
too
cumbersome. I think that an XSLT transform will be more direct. I just
don't
have the time right now to write it. Since this is used so frequently I
was
hoping that there might already be a Framework function to do this.

Kevin

"Peter IEFlynn" wrote:
Kevin Burton wrote:

> I am sure this has been asked enough to warrant an FAQ but I could not
> find it.
>
> Does anyone have some code and maybe an XSLT stylesheet that would
> allow
> me to transform an XML string to a "pretty" version (nodes indented on
> separate lines, etc.)?

Open it in IE or Firefox.
Or use an XML editor that has an indent-and-reformat function.

///Peter
--
XML FAQ: http://xml.silmaril.ie/


Nov 12 '05 #5
You may also do as simple as this...

XmlDocument dom = new XmlDocument();
dom.Load("not-pretty.xml");
dom.Save("pretty.xml");

If you don't want any I/O operations, you may also Save to TextWriter or
Stream

Thanks
Pranav

"Kevin Burton" <Ke*********@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
This was precisly what I was looking for. Thank you.

"Kent Boogaart" wrote:
I think this is what you want. Probably a more efficient way to do it...

using System;

using System.IO;

using System.Xml;

namespace XmlTest

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

using (StringWriter stringWriter = new StringWriter())

{

XmlDocument doc = new XmlDocument();

//get your document

doc.LoadXml("<root><element
foo=\"bar\"><child></child></element></root>");

//create reader and writer

XmlNodeReader xmlReader = new XmlNodeReader(doc);

XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);

//set formatting options

xmlWriter.Formatting = Formatting.Indented;

xmlWriter.Indentation = 1;

xmlWriter.IndentChar = '\t';

//write the document formatted

xmlWriter.WriteNode(xmlReader, true);

Console.WriteLine(stringWriter.ToString());

}
Console.ReadLine();

}

}

}

Cheers,

Kent

"Kevin Burton" <Ke*********@discussions.microsoft.com> wrote in message
news:62**********************************@microsof t.com...
>
> I want to put the "pretty" XML in an .NET RTF Text box. Is there an
> XSLT
> transform or some .NET Framework function that I can call? In VB there
> is
> the
> "Indent" function with the MSXML toolkit, but I am using C#. I would
> like
> a
> more direct approach. Spawning IE and trying to get the text out would
> be
> too
> cumbersome. I think that an XSLT transform will be more direct. I just
> don't
> have the time right now to write it. Since this is used so frequently I
> was
> hoping that there might already be a Framework function to do this.
>
> Kevin
>
> "Peter IEFlynn" wrote:
>
>> Kevin Burton wrote:
>>
>> > I am sure this has been asked enough to warrant an FAQ but I could
>> > not
>> > find it.
>> >
>> > Does anyone have some code and maybe an XSLT stylesheet that would
>> > allow
>> > me to transform an XML string to a "pretty" version (nodes indented
>> > on
>> > separate lines, etc.)?
>>
>> Open it in IE or Firefox.
>> Or use an XML editor that has an indent-and-reformat function.
>>
>> ///Peter
>> --
>> XML FAQ: http://xml.silmaril.ie/
>>
>>
>>


Nov 12 '05 #6

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

Similar topics

4
by: Guenther Schmidt | last post by:
Hi, is it possible to have PHP pretty print the HTML output? Günther
2
by: Urs Muntwyler | last post by:
Hi all I wonder if there is really no way to pretty print a DOM, using Java's standard API (http://java.sun.com/j2se/1.4.2/docs/api/index.html). What I've tried is (assume having a...
5
by: Humpdydum | last post by:
Any freeware/open-source that does that? Windows- OR Linux-based is fine. On Linux it would be s/t like "pyprint -r *.py > out.ps" where -r indicates recursive. Done a search on google and nothing...
0
by: Andy Fish | last post by:
Hi, I am trying to use MXXMLWriter with the indent flag to pretty print an XML document from vb6. Although the output is mostly indented in a reasonable way, the end tags are always bunched...
0
by: samlee | last post by:
Hi, All, Is there any free utility for pretty print (reformating) c# source files. TIA,
2
by: akbar | last post by:
Hi, I have Document. If I print it like this: print doc.toprettyxml(" ") I will get this: <a> <b> <c>
4
by: MooMaster | last post by:
I'm trying to develop a little script that does some string manipulation. I have some few hundred strings that currently look like this: cond(a,b,c) and I want them to look like this: ...
1
by: Simon Kittle | last post by:
Hi, Is it possible in PHP to provide a nice pretty page when a fatal error occurs? (Or any of the errors listed here http://uk.php.net/manual/en/function.set-error-handler.php which you cannot...
1
by: rh0dium | last post by:
Hi all, Perhaps it's not supposed to work like this but I thought if you supplied a width to pprint it would nicely format a list to the width. KEYS = pp = pprint.PrettyPrinter(indent=2)...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.