30 January 2007

How to convert XmlDocument to XmlReader for SqlXml data type.

The SqlXml datatype constructor only accepts a Stream or XmlReader, if you have an XmlDocument in memory (go with stream if you have it) then you can easily convert to a SqlXml compatible stream with the XmlNodeReader class.

XmlNodeReader implements IDisposable, so wrap it in a using or try/catch.

For example:

XmlDocument xml = new XmlDocument();
using (XmlNodeReader xnr = new XmlNodeReader(xml))
{
SqlXml sx = new SqlXml(xnr);
}

Filed under: , , ,
 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Stuart Ballard said:

This is great information and very timely - I was just faced with exactly the need to go from an XmlDocument to an XmlReader in a different context, where I want to use XSLT to transform an XmlDocument in-memory. The approach I'd been using was to write out the document to a StringBuilder, make an XmlReader from the string, run the transform from the XmlReader to an XmlWriter against ANOTHER StringBuilder, and then load that back into an XmlDocument.

Of course prefaced by a large comment about how completely STUPID that is and that there MUST be a better way than to have to serialize a document into a string TWICE in order to transform it.

XmlNodeReader is a fantastic thing to know about to eliminate the first of those StringBuilders, and of course I've updated my code accordingly. But I can't find an equivalent class for the other half; I can't find an XmlDocumentWriter class that I can pass into the Transform method to produce an XmlDocument in memory without going via string representation. Does such a thing exist?

31 January 07 at 11:19 AM
# jongallant said:

Stuart - If I understand what you are looking for, you should use the Load method of XmlDocument, passing it the reader that is returned from the Transform method.

31 January 07 at 12:30 PM
# Stuart Ballard said:

I may be missing something but I couldn't find a Transform method that returned a reader - they all seemed to want to take a writer or stream or filename to output to instead. But I'll look again. Thanks for the quick response :)

31 January 07 at 2:27 PM
# jongallant said:

I see what you are asking for now.  I don't believe an XmlNodeWriter exists in the framework, but you could try this:

http://msdn2.microsoft.com/en-us/library/aa302299.aspx#xmltoolsupdate_topic2

http://www.gotdotnet.com/community/usersamples/details.aspx?sampleguid=8f9de8a7-fec9-48e1-91c5-1fc569cde11c

31 January 07 at 2:42 PM
# Stuart Ballard said:

Thanks! According to the comments on the gotdotnet page, it appears that writing out to a string is in fact comparable or better in efficiency than the posted XmlNodeWriter. I still hope Microsoft will add an efficient implementation in future framework versions, but in the meantime I'll save on the complexity and live with the ugly but serviceable code I have. Adding all that code for the sake of reduced overhead which may not actually materialize doesn't seem worth it. My code isn't performance critical anyway.

31 January 07 at 4:38 PM
# Stuart Ballard said:

Someone on the XML team blog pointed out that there is in fact an implementation of XmlNodeWriter in the framework. doc.CreateNavigator().AppendChild() returns it. Strange place to put it, but it's there :)

07 February 07 at 12:23 PM
# IHateBill said:

I've been trying for days now to contruct a SQLXML type using a stream or an xmlreader, PER MICROSOFT documentation.  Won't work...no errors, just an empty SQLXML variable.  Tried creating a file stream or loading an xmldocument from a path & persisting to a stream (can load a different xml document from the persisted stream, but not the gd'd sqlxml type.

Using the node reader works!  Where the hell is this documented that MS documentation is a bunch crap, that you have to stand on your head, while picking your nose and toucihng your toes to initialize this stupid type?

There needs to be a website: www.BillBackBillForWastedTime.com

Can we just have COM back?  Despite the ms propaganda, previous Visual Studio incarnations can in fact still do the stuff that microsoft said it could do prior to .net, but now allegedly can't do as of .net....and faster, at least you can persist a domdoc to an object implelenting IStream & know that you could pull the same xml out of that object...and not have to spend days trying to figure out what the hell was going through the minds of the .net team of jackasses (a horrible insult to stuborn, stupid animals...hope no animal lovers flame me over this!)

03 November 07 at 2:28 PM
# arunganu said:

Hi,

Related to the same subject I tried to get a SqlXml object using a XmlReader object.

The XmlReader instance is built using a StringReader object which wraps

a xml-like string that contains the xml declaration:

<?xml version='1.0' encoding='utf-8' ?>

However the Value (read-only) of SqlXml instance ( that will be output

by a CLR stored procedure) contains the whole xml document but the Xml declaration

I also tried with settings set to ConformanceLevel.Document but got the

same results

Could you please let me know how could I get the xml declaration inside

the SqlXml object ? Please find the code below

Many thanks,

Ted

string strxml = @"<?xml version='1.0' encoding='utf-8' ?><!-- Comment

--><bookstore><book><title>Art</title><price>10</price></book><book><title>Furniture</title><price>15</price></book></bookstore>";

StringReader sr = new StringReader(strxml);

XmlReaderSettings settings = new XmlReaderSettings();

settings.ConformanceLevel = ConformanceLevel.Auto;

XmlReader reader0 = XmlReader.Create(sr, settings);

SqlXml sqlxml = new SqlXml(reader0);

reader0.Close();

string str = sqlxml.Value;  // contains all xml content but the xml declaration

25 November 08 at 1:37 PM
# arunganu said:

Hi,

Related to te same subject I tried to get a SqlXml object using a XmlReader object.

The XmlReader instance is built using a StringReader object which wraps

a xml-like string that contains the xml declaration:

<?xml version='1.0' encoding='utf-8' ?>

However the Value (read-only) of SqlXml instance ( that will be output

by a CLR stored procedure) contains the whole xml document but the Xml

declaration

I also tried with settings set to ConformanceLevel.Document but got the

same results

Could you please let me know how could I get the xml declaration inside

the SqlXml object ? Please find the code below

Many thanks,

Ted

string strxml = @"<?xml version='1.0' encoding='utf-8' ?><!-- Comment

--><bookstore><book><title>Art</title><price>10</price></book><book><title>Furniture</title><price>15</price></book></bookstore>";

StringReader sr = new StringReader(strxml);

XmlReaderSettings settings = new XmlReaderSettings();

settings.ConformanceLevel = ConformanceLevel.Auto;

XmlReader reader0 = XmlReader.Create(sr, settings);

SqlXml sqlxml = new SqlXml(reader0);

reader0.Close();

string str = sqlxml.Value;  // contains all xml content but the xml declaration

25 November 08 at 2:00 PM

Leave a Comment

Comment Policy: No HTML allowed. URIs and line breaks are converted automatically. Your e–mail address will not show up on any public page.

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Page view tracker