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
way to format into a indented string? TIA
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
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.
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.
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();
}
}
This, I expected you to do..