Blog Stats
  • Posts - 42
  • Articles - 0
  • Comments - 61
  • Trackbacks - 36

 

XML datatype in SQL server 2005 - just be sure

In my last post I mentioned that SQL Server 2005 provides with you with the flexibility of having an XML document stored into one single column of “Xml” datatype. Just like a int, datetime or a nvarchar() datatype, you can define “Xml” datatype to store the whole Xml document or a fragment as long as your XML is valid.

But the catch here is if your XML file has a header something like “ “, and you are sending this document as a string to the database, you are in trouble. You are presented with an exception something like “XML parsing: line 1, character 38, unable to switch the encoding”.

So the solution is, while you are opening the FileStream, open with a second parameter “Encoding.Unicode”. That's it. So now your XML document is going to be stored in Unicode (UTF-16) format. However when you retrieve it back from database, remember to convert it to UTF-8, if you are expecting it.

It pays to use http://groups.google.com

Check out limitations with “XML datatype“ in SQL server 2005

Update: Or better still, you are better off using the following.

m_sqlCommand.Parameters["@XmlDocument"].Value = new SqlXml(new XmlTextReader(xmlDocumentToPush, XmlNodeType.Document, null));


Feedback

# re: XML datatype in SQL server 2005 - just be sure

Gravatar thanks
8/13/2007 2:50 PM | kiruba

# re: XML datatype in SQL server 2005 - just be sure

Gravatar I have no idea what you just said.

How would I go about "setting" the encoding?

Pete 8/17/2007 11:17 AM | Pete

# re: XML datatype in SQL server 2005 - just be sure

Gravatar This worked for me:

StringReader transactionXml = new StringReader(myXmlString);
XmlTextReader xmlReader = new XmlTextReader(transactionXml);
SqlXml sqlXml = new SqlXml(xmlReader);

Then use the sqlXml in your insert/update statement. 4/23/2008 6:34 AM | Riaan

# re: XML datatype in SQL server 2005 - just be sure

Gravatar i hav a xml file that i hav to store in a database(sql) can any 1 give me the code or idea.
i m new in xml 7/28/2008 2:46 AM | chanchal

# re: XML datatype in SQL server 2005 - just be sure

Gravatar thanks a lot 8/8/2008 6:16 AM | sezai

# re: XML datatype in SQL server 2005 - just be sure

Gravatar Here are some tips for working with xml data previously stored as text (ntext), or varchar(nvarchar):

if your saved xml has the encoding="utf-16" in its string, you need to convert that whole xml string to proper type:

utf-16 ---> ntext, or nvarchar(max), or nvarbinary(max). These are unicode (2-byte) string representations.

utf-8 ---> text or varchar(max), or varbinary(max). These are single-byte string representations.

Wrong conversion would result in the that cryptic "XML parsing: line 1, character 38, unable to switch the encoding" message.

Happy coding.

9/9/2008 10:02 AM | Steve Yang

# re: XML datatype in SQL server 2005 - just be sure

Gravatar Thank You 2/13/2009 10:20 AM | Kumar

# re: XML datatype in SQL server 2005 - just be sure

Gravatar Thanks a lot, you saved my time! 2/24/2009 12:37 PM | Vladimir

# re: XML datatype in SQL server 2005 - just be sure

Gravatar Hi, the one i tried below using above replies is giving me following error "Could not find file 'C:\WINDOWS\system32\System.Xml.XmlDocument'."

SqlParameter parameter2 = new SqlParameter();
parameter2.ParameterName = "@XML_Content";
parameter2.SqlDbType = SqlDbType.Xml;
parameter2.Direction = ParameterDirection.Input;
parameter2.Value = new SqlXml(new XmlTextReader(new StreamReader(xmlContent.ToString().Trim())));

In the above code xmlContent is XmlDocument. This value has to go into a column in database which is of XML type and the paramter here i am setting is also XML type i.e. storedprocedure parmater which take this value is of XML type. Can any body tell me what going wrong?
3/13/2009 9:19 AM | Pradeep

# re: XML datatype in SQL server 2005 - just be sure

Gravatar Good one, Thank You 6/5/2009 11:11 AM | Srikrishna

Post a comment





 

 

 

Copyright © Vinayak