all groups > dotnet xml > november 2003 >
You're in the

dotnet xml

group:

newbie: pretty print of xml to memory string


newbie: pretty print of xml to memory string Yechezkal Gutfreund
11/12/2003 7:55:59 PM
dotnet xml: I am sure this a newbie-type question.

But I was looking to see if someone had something that walks an xml tree and
creates a nicely tabbed (pretty print) string version of the xml tree
(probably starting from a node, like innerxml). But innerxml sometimes (when
preservewhitespace is off) return a single very very long line.

Is there a quickie XSLT for this? or something else (I seem to remember and
output directive) but I want this from
C#.

RE: newbie: pretty print of xml to memory string v-kevy NO[at]SPAM online.microsoft.com
11/13/2003 10:01:54 AM
Hi Yechezkal,

The simplest way to return a nicely tabbed string, I think is to write a
recursive loop to format it. I've written a short example to achieve this.
When calling, you can use string str = print(xmldocument.DocumentElement,
0);

private string print(XmlNode n, int layer)
{
string s = string.Empty;
for(int i=0; i<layer; i++)
s += "\t";
if(n.Value != null)
s += n.Value + System.Environment.NewLine;
foreach(XmlNode sub in n.ChildNodes)
s += print(sub, layer+1);
return s;
}

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Yechezkal Gutfreund" <sgutfreund@hotmail.com>
| Subject: newbie: pretty print of xml to memory string
| Date: Wed, 12 Nov 2003 19:55:59 -0500
| Lines: 12
| Organization: Kesser Technical Group
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <uoBDtDYqDHA.2808@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.xml
| NNTP-Posting-Host: 207-180-183-54.c3-0.abr-ubr1.sbo-abr.ma.cable.rcn.com
207.180.183.54
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.xml:17181
| X-Tomcat-NG: microsoft.public.dotnet.xml
|
| I am sure this a newbie-type question.
|
| But I was looking to see if someone had something that walks an xml tree
and
| creates a nicely tabbed (pretty print) string version of the xml tree
| (probably starting from a node, like innerxml). But innerxml sometimes
(when
| preservewhitespace is off) return a single very very long line.
|
| Is there a quickie XSLT for this? or something else (I seem to remember
and
| output directive) but I want this from
| C#.
|
|
|
Re: newbie: pretty print of xml to memory string Oleg Tkachenko
11/13/2003 1:59:49 PM
[quoted text, click to view]

Use XmlTextWriter with Formatting property set to Formatting.Indented.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Re: newbie: pretty print of xml to memory string Amol Kher [MSFT]
11/24/2003 11:54:42 AM
Have you tried, Formatting Property on XmlTextWriter? You can say

writer.Formatting = Formatting.Indented.

-Amol

[quoted text, click to view]

AddThis Social Bookmark Button