472,960 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,960 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 15694
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.