Xml formatted output

  • Thread starter Thread starter William Stacey [MVP]
  • Start date Start date
W

William Stacey [MVP]

Give some string of xml ( maybe one long unformatted line), what is quick
way to format into a indented string? TIA
 
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 said:
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
 
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 said:
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.
 
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 said:
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.
 
Very correct..

This, I expected you to do..

William Stacey said:
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();
}
}
 
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!
 
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."
 
Back
Top