Xml formatted output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Stacey [MVP]

    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


  • Sahil Malik

    #2
    Re: Xml formatted output

    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]" <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]


    Comment

    • Champika Nirosh

      #3
      Re: Xml formatted output

      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=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]


      Comment

      • Sahil Malik

        #4
        Re: Xml formatted output

        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" <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]


        Comment

        • William Stacey [MVP]

          #5
          Re: Xml formatted output

          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]

          Comment

          • Champika Nirosh

            #6
            Re: Xml formatted output

            Very correct..

            This, I expected you to do..

            "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]


            Comment

            • William Stacey [MVP]

              #7
              Re: Xml formatted output

              > 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!

              --
              William Stacey, MVP


              Comment

              • Kevin Yu [MSFT]

                #8
                Re: Xml formatted output

                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."

                Comment

                Working...