473,320 Members | 1,920 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.

Xml formatted output

Give some string of xml ( maybe one long unformatted line), what is quick
way to format into a indented string? TIA

--
William Stacey, MVP
Nov 16 '05 #1
7 2527
William,

Two tricks come to mind

a) XSLT
b) Recursive function with an string strIndent static variable that appends
itself with a space everytime it recurses.

- Sahil Malik
You can reach me thru my blog http://www.dotnetjunkies.com/weblog/sahilmalik
"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
Give some string of xml ( maybe one long unformatted line), what is quick
way to format into a indented string? TIA

--
William Stacey, MVP

Nov 16 '05 #2
Hi,

The easiest is to create a XmlDocument with a single parent Node and add the
whole string as the InerXml of the node..

May be ..

XmlDoc.SelectSingleNode("ParentNode").InnerXml = You unformatted string;

then

XmlDoc.Save("New File Name");

This will give you a good fomatted XML..

Nirosh.

"Sahil Malik" <co*****************@nospam.com> wrote in message
news:el*************@TK2MSFTNGP15.phx.gbl...
William,

Two tricks come to mind

a) XSLT
b) Recursive function with an string strIndent static variable that appends itself with a space everytime it recurses.

- Sahil Malik
You can reach me thru my blog http://www.dotnetjunkies.com/weblog/sahilmalik

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
Give some string of xml ( maybe one long unformatted line), what is quick way to format into a indented string? TIA

--
William Stacey, MVP


Nov 16 '05 #3
Hmmm Champika,

Maybe William didn't want to see the XML tags in the output - atleast that
is what I assumed.

If he does want to see the tags, why not do a save from the original
XMLDocument? (Or stream if not save).

- Sahil Malik
You can reach me thru my blog http://www.dotnetjunkies.com/weblog/sahilmalik
"Champika Nirosh" <te**@test.lk> wrote in message
news:OH*************@tk2msftngp13.phx.gbl...
Hi,

The easiest is to create a XmlDocument with a single parent Node and add
the
whole string as the InerXml of the node..

May be ..

XmlDoc.SelectSingleNode("ParentNode").InnerXml = You unformatted string;

then

XmlDoc.Save("New File Name");

This will give you a good fomatted XML..

Nirosh.

"Sahil Malik" <co*****************@nospam.com> wrote in message
news:el*************@TK2MSFTNGP15.phx.gbl...
William,

Two tricks come to mind

a) XSLT
b) Recursive function with an string strIndent static variable that

appends
itself with a space everytime it recurses.

- Sahil Malik
You can reach me thru my blog

http://www.dotnetjunkies.com/weblog/sahilmalik


"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
> Give some string of xml ( maybe one long unformatted line), what is quick > way to format into a indented string? TIA
>
> --
> William Stacey, MVP
>
>



Nov 16 '05 #4
Probably an easier/faster way, but this works as a generic method:

public static string GetFormattedXML(string xml)
{
using (StringWriter sw = new StringWriter() )
{
XmlTextWriter xw = new XmlTextWriter(sw);
xw.Formatting = Formatting.Indented;
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
xmldoc.WriteTo(xw);
xw.Close();
return sw.ToString();
}
}

--
William Stacey, MVP

"Champika Nirosh" <te**@test.lk> wrote in message
news:OH*************@tk2msftngp13.phx.gbl...
Hi,

The easiest is to create a XmlDocument with a single parent Node and add the whole string as the InerXml of the node..

May be ..

XmlDoc.SelectSingleNode("ParentNode").InnerXml = You unformatted string;

then

XmlDoc.Save("New File Name");

This will give you a good fomatted XML..

Nirosh.

"Sahil Malik" <co*****************@nospam.com> wrote in message
news:el*************@TK2MSFTNGP15.phx.gbl...
William,

Two tricks come to mind

a) XSLT
b) Recursive function with an string strIndent static variable that

appends
itself with a space everytime it recurses.

- Sahil Malik
You can reach me thru my blog

http://www.dotnetjunkies.com/weblog/sahilmalik


"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
Give some string of xml ( maybe one long unformatted line), what is quick way to format into a indented string? TIA

--
William Stacey, MVP




Nov 16 '05 #5
Very correct..

This, I expected you to do..

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:ul**************@TK2MSFTNGP15.phx.gbl...
Probably an easier/faster way, but this works as a generic method:

public static string GetFormattedXML(string xml)
{
using (StringWriter sw = new StringWriter() )
{
XmlTextWriter xw = new XmlTextWriter(sw);
xw.Formatting = Formatting.Indented;
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
xmldoc.WriteTo(xw);
xw.Close();
return sw.ToString();
}
}

--
William Stacey, MVP

"Champika Nirosh" <te**@test.lk> wrote in message
news:OH*************@tk2msftngp13.phx.gbl...
Hi,

The easiest is to create a XmlDocument with a single parent Node and add

the
whole string as the InerXml of the node..

May be ..

XmlDoc.SelectSingleNode("ParentNode").InnerXml = You unformatted string;

then

XmlDoc.Save("New File Name");

This will give you a good fomatted XML..

Nirosh.

"Sahil Malik" <co*****************@nospam.com> wrote in message
news:el*************@TK2MSFTNGP15.phx.gbl...
William,

Two tricks come to mind

a) XSLT
b) Recursive function with an string strIndent static variable that

appends
itself with a space everytime it recurses.

- Sahil Malik
You can reach me thru my blog

http://www.dotnetjunkies.com/weblog/sahilmalik


"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:eB**************@TK2MSFTNGP12.phx.gbl...
> Give some string of xml ( maybe one long unformatted line), what is

quick
> way to format into a indented string? TIA
>
> --
> William Stacey, MVP
>
>


Nov 16 '05 #6
> Very correct..
This, I expected you to do..


Not sure how to read that Champika? :-) To be clear, I meant there is
probably and easier way then what I posted. Also during the time waiting
for reply, I stated playing so just wanted to post this back. Was not
trying to step on any posts. Cheers!

--
William Stacey, MVP
Nov 16 '05 #7
Hi William,

I think your code is fine. That might be the simplest way that I can
thought. Cheers!

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

Nov 16 '05 #8

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

Similar topics

12
by: Craig Thomson | last post by:
Do people generally try and output nicely formatted html from their scripts? I have been trying to, with variable indenting for tables etc. It makes it easier to see the html and perhaps debug...
2
by: Mark | last post by:
How do I output formatted numerical data to an external (text) file. At a minimum I want to specify the number of decimal places to print. Ideally I'd like the type of control the fortran FORMAT...
8
by: Matthew Thorley | last post by:
Greetings, perhaps someone can explain this. I get to different styles of formatting for xmla and xmlb when I do the following: from elementtree import ElementTree as et xmla =...
4
by: Joe C | last post by:
I've written a console application and would like to isolate all screen output so that it will be easier to migrate the code to a GUI-type platform without modification to the base code. As a...
6
by: Jason Heyes | last post by:
I am starting to worry about the performance of formatted read/write operations on data-redundant objects in my program.What can I do to improve this performance should it become an issue? ...
2
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent:...
6
by: Magix | last post by:
Hi, I want to use fprintf to write to a file. My question about the formatted output How can I format so that I can allocate certain width for each %s (Left-aignlied) ? Example: fprintf("%s...
3
by: Craig Petrie | last post by:
Hi, I have a large table in Word 2003 that has formatted text in the cells and wish to read and convert a cells formatted contents to html output via vb.net code. The formatting contains the...
6
by: Jojo | last post by:
Hi all, I was wondering how I can perform formatted output with C++ strings. For example, suppose I have in plain C: sprintf(C_string, "%5.2f %6d"); How can I do such a thing with C++...
4
by: keith | last post by:
I've been beating my head against this for a little while, so perhaps someone can help me out here? The code below should output exactly as follows (the hex data lines up in a fixed font): DATA...
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...
1
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.