Convert XmlReader to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • muesliflakes

    Convert XmlReader to string

    I wish to receive some data from SqlServer as XML and write out result
    to as a string.

    Eg: cmd = new SqlCommand( "select * from blah for xml auto", con );

    XmlReader result = cmd.ExecuteXmlR eader( );

    Console.Writeln ( result.ToString ( ) );

    This XML is meant to be the input for an XSLT transform so I'm
    interested in seeing the XML in a form similar to <row col1='blah'
    col2='blah' />. The XML comes back ok, but I'm not sure how to convert
    the XmlReader into a String in an efficient manner.

    One way I know would be to load it into a dataset and then write out
    as XML and another way would be to serialize / deserialize from an
    object, but surely there is a simple way of converting the XmlReader
    into a String.

    Cheers Dave
  • Jon Skeet [C# MVP]

    #2
    Re: Convert XmlReader to string

    muesliflakes <muesliflakes@y ahoo.com.au> wrote:[color=blue]
    > I wish to receive some data from SqlServer as XML and write out result
    > to as a string.
    >
    > Eg: cmd = new SqlCommand( "select * from blah for xml auto", con );
    >
    > XmlReader result = cmd.ExecuteXmlR eader( );
    >
    > Console.Writeln ( result.ToString ( ) );
    >
    > This XML is meant to be the input for an XSLT transform so I'm
    > interested in seeing the XML in a form similar to <row col1='blah'
    > col2='blah' />. The XML comes back ok, but I'm not sure how to convert
    > the XmlReader into a String in an efficient manner.
    >
    > One way I know would be to load it into a dataset and then write out
    > as XML and another way would be to serialize / deserialize from an
    > object, but surely there is a simple way of converting the XmlReader
    > into a String.[/color]

    The easiest thing is to read the whole document in using
    XmlDocument.Loa d(XmlReader) and then either write it out again using an
    XmlWriter writing to a StringWriter, or something similar. (I haven't
    tried calling OuterXml on an XmlDocument - that might do it for you in
    a very straightforward way.)

    Of course, this is a somewhat inefficient way of doing things. An
    alternative is to have an XmlWriter writing to a StringWriter and use
    XmlWriter.Write Node (XmlReader, bool). I can't say I've tried that
    either, to be honest - but it's worth having a go.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • d c

      #3
      Re: Convert XmlReader to string

      I would love to use the XmlDocument class but the problem is that this
      class requires the XmlReader to return a well formed document.

      My query which is to get the structure of a table:

      select * from information_sch ema.columns where table_name = 'MtFeedback'
      for xml auto

      Does not return a well formed document, it only returns an XML fragment.

      No Root Element:
      <information_sc hema.columns TABLE_CATALOG=" Mt" TABLE_SCHEMA="d bo"
      TABLE_NAME="MtF eedback" COLUMN_NAME="Mt FeedbackIy" ORDINAL_POSITIO N="1"
      IS_NULLABLE="No " DATA_TYPE="int" NUMERIC_PRECISI ON="10"
      NUMERIC_PRECISI ON_RADIX="10" NUMERIC_SCALE=" 0"/>

      <information_sc hema.columns TABLE_CATALOG=" Mt" TABLE_SCHEMA="d bo"
      TABLE_NAME="MtF eedback" COLUMN_NAME="Ti tle" ORDINAL_POSITIO N="2"
      IS_NULLABLE="YE S" DATA_TYPE="varc har" CHARACTER_MAXIM UM_LENGTH="50"
      CHARACTER_OCTET _LENGTH="50" CHARACTER_SET_N AME="iso_1"
      COLLATION_NAME= "Latin1_General _CI_AS"/>

      etc...


      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      Working...