469,292 Members | 1,454 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

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 15515
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Guenther Schmidt | last post: by
2 posts views Thread by Urs Muntwyler | last post: by
5 posts views Thread by Humpdydum | last post: by
reply views Thread by Andy Fish | last post: by
reply views Thread by samlee | last post: by
2 posts views Thread by akbar | last post: by
4 posts views Thread by MooMaster | last post: by
1 post views Thread by Simon Kittle | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.