"William Stacey [MVP]" <staceywREMOVE@ mvps.org> wrote in message
news:eBeHKwwtEH A.2668@TK2MSFTN GP12.phx.gbl...[color=blue]
> Give some string of xml ( maybe one long unformatted line), what is quick
> way to format into a indented string? TIA
>
> --
> William Stacey, MVP
>
>[/color]
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.SelectSi ngleNode("Paren tNode").InnerXm l = You unformatted string;
then
XmlDoc.Save("Ne w File Name");
This will give you a good fomatted XML..
Nirosh.
"Sahil Malik" <contactmethrum yblog@nospam.co m> wrote in message
news:elAeKtxtEH A.224@TK2MSFTNG P15.phx.gbl...[color=blue]
> William,
>
> Two tricks come to mind
>
> a) XSLT
> b) Recursive function with an string strIndent static variable that[/color]
appends[color=blue]
> itself with a space everytime it recurses.
>
> - Sahil Malik
> You can reach me thru my blog[/color] http://www.dotnetjunkies.com/weblog/sahilmalik[color=blue]
>
>
> "William Stacey [MVP]" <staceywREMOVE@ mvps.org> wrote in message
> news:eBeHKwwtEH A.2668@TK2MSFTN GP12.phx.gbl...[color=green]
> > Give some string of xml ( maybe one long unformatted line), what is[/color][/color]
quick[color=blue][color=green]
> > way to format into a indented string? TIA
> >
> > --
> > William Stacey, MVP
> >
> >[/color]
>
>[/color]
"Champika Nirosh" <test@test.lk > wrote in message
news:OHhoTvytEH A.348@tk2msftng p13.phx.gbl...[color=blue]
> 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.SelectSi ngleNode("Paren tNode").InnerXm l = You unformatted string;
>
> then
>
> XmlDoc.Save("Ne w File Name");
>
> This will give you a good fomatted XML..
>
> Nirosh.
>
> "Sahil Malik" <contactmethrum yblog@nospam.co m> wrote in message
> news:elAeKtxtEH A.224@TK2MSFTNG P15.phx.gbl...[color=green]
>> William,
>>
>> Two tricks come to mind
>>
>> a) XSLT
>> b) Recursive function with an string strIndent static variable that[/color]
> appends[color=green]
>> itself with a space everytime it recurses.
>>
>> - Sahil Malik
>> You can reach me thru my blog[/color]
> http://www.dotnetjunkies.com/weblog/sahilmalik[color=green]
>>
>>
>> "William Stacey [MVP]" <staceywREMOVE@ mvps.org> wrote in message
>> news:eBeHKwwtEH A.2668@TK2MSFTN GP12.phx.gbl...[color=darkred]
>> > Give some string of xml ( maybe one long unformatted line), what is[/color][/color]
> quick[color=green][color=darkred]
>> > way to format into a indented string? TIA
>> >
>> > --
>> > William Stacey, MVP
>> >
>> >[/color]
>>
>>[/color]
>
>[/color]
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(s w);
xw.Formatting = Formatting.Inde nted;
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml( xml);
xmldoc.WriteTo( xw);
xw.Close();
return sw.ToString();
}
}
--
William Stacey, MVP
"Champika Nirosh" <test@test.lk > wrote in message
news:OHhoTvytEH A.348@tk2msftng p13.phx.gbl...[color=blue]
> Hi,
>
> The easiest is to create a XmlDocument with a single parent Node and add[/color]
the[color=blue]
> whole string as the InerXml of the node..
>
> May be ..
>
> XmlDoc.SelectSi ngleNode("Paren tNode").InnerXm l = You unformatted string;
>
> then
>
> XmlDoc.Save("Ne w File Name");
>
> This will give you a good fomatted XML..
>
> Nirosh.
>
> "Sahil Malik" <contactmethrum yblog@nospam.co m> wrote in message
> news:elAeKtxtEH A.224@TK2MSFTNG P15.phx.gbl...[color=green]
> > William,
> >
> > Two tricks come to mind
> >
> > a) XSLT
> > b) Recursive function with an string strIndent static variable that[/color]
> appends[color=green]
> > itself with a space everytime it recurses.
> >
> > - Sahil Malik
> > You can reach me thru my blog[/color]
> http://www.dotnetjunkies.com/weblog/sahilmalik[color=green]
> >
> >
> > "William Stacey [MVP]" <staceywREMOVE@ mvps.org> wrote in message
> > news:eBeHKwwtEH A.2668@TK2MSFTN GP12.phx.gbl...[color=darkred]
> > > Give some string of xml ( maybe one long unformatted line), what is[/color][/color]
> quick[color=green][color=darkred]
> > > way to format into a indented string? TIA
> > >
> > > --
> > > William Stacey, MVP
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]
"William Stacey [MVP]" <staceywREMOVE@ mvps.org> wrote in message
news:ul2frh3tEH A.3872@TK2MSFTN GP15.phx.gbl...[color=blue]
> 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(s w);
> xw.Formatting = Formatting.Inde nted;
> XmlDocument xmldoc = new XmlDocument();
> xmldoc.LoadXml( xml);
> xmldoc.WriteTo( xw);
> xw.Close();
> return sw.ToString();
> }
> }
>
> --
> William Stacey, MVP
>
> "Champika Nirosh" <test@test.lk > wrote in message
> news:OHhoTvytEH A.348@tk2msftng p13.phx.gbl...[color=green]
> > Hi,
> >
> > The easiest is to create a XmlDocument with a single parent Node and add[/color]
> the[color=green]
> > whole string as the InerXml of the node..
> >
> > May be ..
> >
> > XmlDoc.SelectSi ngleNode("Paren tNode").InnerXm l = You unformatted string;
> >
> > then
> >
> > XmlDoc.Save("Ne w File Name");
> >
> > This will give you a good fomatted XML..
> >
> > Nirosh.
> >
> > "Sahil Malik" <contactmethrum yblog@nospam.co m> wrote in message
> > news:elAeKtxtEH A.224@TK2MSFTNG P15.phx.gbl...[color=darkred]
> > > William,
> > >
> > > Two tricks come to mind
> > >
> > > a) XSLT
> > > b) Recursive function with an string strIndent static variable that[/color]
> > appends[color=darkred]
> > > itself with a space everytime it recurses.
> > >
> > > - Sahil Malik
> > > You can reach me thru my blog[/color]
> > http://www.dotnetjunkies.com/weblog/sahilmalik[color=darkred]
> > >
> > >
> > > "William Stacey [MVP]" <staceywREMOVE@ mvps.org> wrote in message
> > > news:eBeHKwwtEH A.2668@TK2MSFTN GP12.phx.gbl...
> > > > Give some string of xml ( maybe one long unformatted line), what is[/color]
> > quick[color=darkred]
> > > > way to format into a indented string? TIA
> > > >
> > > > --
> > > > William Stacey, MVP
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>[/color]
> Very correct..[color=blue]
> This, I expected you to do..[/color]
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!
Comment