sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Kevin Burton's Avatar

Pretty print XML?


Question posted by: Kevin Burton (Guest) on November 12th, 2005 05:09 AM
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
5 Answers Posted
Peter Flynn's Avatar
Guest - n/a Posts
#2: Re: Pretty print XML?

Kevin Burton wrote:
[color=blue]
> 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.)?[/color]

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/


Kevin Burton's Avatar
Guest - n/a Posts
#3: Re: Pretty print XML?


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:
[color=blue]
> Kevin Burton wrote:
>[color=green]
> > 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.)?[/color]
>
> 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/
>
>
>[/color]
Kent Boogaart's Avatar
Guest - n/a Posts
#4: Re: Pretty print XML?

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" <KevinBurton@discussions.microsoft.com> wrote in message
news:6211C93E-BD9E-4157-815E-368F7222C62D@microsoft.com...[color=blue]
>
> 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:
>[color=green]
>> Kevin Burton wrote:
>>[color=darkred]
>> > 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.)?[/color]
>>
>> 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/
>>
>>
>>[/color][/color]


Kevin Burton's Avatar
Guest - n/a Posts
#5: Re: Pretty print XML?

This was precisly what I was looking for. Thank you.

"Kent Boogaart" wrote:
[color=blue]
> 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" <KevinBurton@discussions.microsoft.com> wrote in message
> news:6211C93E-BD9E-4157-815E-368F7222C62D@microsoft.com...[color=green]
> >
> > 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:
> >[color=darkred]
> >> 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/
> >>
> >>
> >>[/color][/color]
>
>
>[/color]
Pranav Kandula's Avatar
Guest - n/a Posts
#6: Re: Pretty print XML?

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" <KevinBurton@discussions.microsoft.com> wrote in message
news:9937F411-11AB-44DD-B1E4-B2C022D9FF24@microsoft.com...[color=blue]
> This was precisly what I was looking for. Thank you.
>
> "Kent Boogaart" wrote:
>[color=green]
>> 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" <KevinBurton@discussions.microsoft.com> wrote in message
>> news:6211C93E-BD9E-4157-815E-368F7222C62D@microsoft.com...[color=darkred]
>> >
>> > 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/
>> >>
>> >>
>> >>[/color]
>>
>>
>>[/color][/color]


 
Not the answer you were looking for? Post your question . . .
196,829 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,829 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors