<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Sushil's WebLog : SQL Server's Xml DataType w/ADO.Net 2.0</title><link>http://blogs.msdn.com/sushilc/archive/tags/SQL+Server_2700_s+Xml+DataType+w_2F00_ADO.Net+2.0/default.aspx</link><description>Tags: SQL Server's Xml DataType w/ADO.Net 2.0</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Typed XML in ADO.Net v2.0</title><link>http://blogs.msdn.com/sushilc/archive/2004/08/04/208250.aspx</link><pubDate>Wed, 04 Aug 2004 20:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:208250</guid><dc:creator>sushilc</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/sushilc/comments/208250.aspx</comments><wfw:commentRss>http://blogs.msdn.com/sushilc/commentrss.aspx?PostID=208250</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;In the earlier sections, we have been looking at Untyped XML which are different from Typed XML. Typed XML data type is an XML defined with&amp;nbsp;an associated xml&amp;nbsp;schema in Sql Server 2005.&amp;nbsp;You may ask if there is anything different on how Typed XML are exposed via ADO.Net 2.0. The answer is a heart-warming NO!. Typed XML are exposed the same way that any untyped XML does through SqlDataReader and SqlParameter. Please note that the check to see if the given XML adheres to the give schema is not done on the client side. This check is done when the statement is executed on the server side.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;There are new set of columns added to the DataTable returned from SqlDataReader.GetSchemaTable() method that give extra information pertaining to the associated XML Schema. The three columns that are added are:&lt;/FONT&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT face=Tahoma&gt;&lt;FONT size=2&gt;XmlSchemaCollectionDatabase:&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;Provides&amp;nbsp;the database in which the schema for this XML type exists.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face=Tahoma&gt;XmlSchemaCollectionOwningSchema:&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;Provides the owning relational schema where the schema for this XML instance is located&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face=Tahoma&gt;XmlSchemaCollectionName: Provides the &lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;name of the schema for this XML type.&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;FONT face=Tahoma&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;Consider the typed XML is defined with the following TSQL:&lt;BR&gt;&lt;FONT color=#0000ff&gt;-- Change to Library database&lt;BR&gt;USE &lt;STRONG&gt;Library&lt;/STRONG&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma&gt;&lt;FONT color=#0000ff&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;-- OrderSchema is defined using the CREATE XML SCHEMA COLLECTION statement&lt;BR&gt;CREATE XML SCHEMA COLLECTION &lt;STRONG&gt;OrderSchema&lt;/STRONG&gt; AS&lt;BR&gt;N'XSD_DEFINITION_HERE'&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;-&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;-&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma color=#0000ff&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;Consider the Schema/namespace&lt;BR&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face=Tahoma&gt;&lt;FONT color=#0000ff&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;-- Customer table is created with a Typed XML column name OrderXml&lt;BR&gt;&lt;/SPAN&gt;CREATE TABLE &lt;STRONG&gt;Customer&lt;/STRONG&gt; (CustomerId int, CustomerName varchar(40), OrderXml xml(OrderSchema))&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;The three-part name of the embedded XML schema can be got as follows:&lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;FONT face=Tahoma color=#0000ff size=2&gt;sqlcommand1.CommandText = "SELECT OrderXml from Customer";&lt;BR&gt;SqlDataReader sqldatareader1 = sqlcommand1.ExecuteReader();&lt;BR&gt;DataTable datatable1 = sqldatareader1.GetSchemaTable();&amp;nbsp;// SchemaTable&lt;BR&gt;Console.WriteLine(datatable1.Rows[0]["XmlSchemaCollectionDatabase"]); //returns &lt;STRONG&gt;Library&lt;/STRONG&gt;&lt;BR&gt;Console.WriteLine(datatable1.Rows[0]["XmlSchemaCollectionOwningSchema"]); //returns &lt;STRONG&gt;dbo&lt;/STRONG&gt;&lt;BR&gt;Console.WriteLine(datatable1.Rows[0]["XmlSchemaCollectionName"]); //retuns &lt;STRONG&gt;OrderSchema&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Disclaimer: This posting is provided "AS IS" with no warranties, and confers no rights&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=208250" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/sushilc/archive/tags/SQL+Server_2700_s+Xml+DataType+w_2F00_ADO.Net+2.0/default.aspx">SQL Server's Xml DataType w/ADO.Net 2.0</category></item><item><title>XML Data type in ADO.Net v2.0 - Part II</title><link>http://blogs.msdn.com/sushilc/archive/2004/08/03/207162.aspx</link><pubDate>Tue, 03 Aug 2004 16:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:207162</guid><dc:creator>sushilc</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/sushilc/comments/207162.aspx</comments><wfw:commentRss>http://blogs.msdn.com/sushilc/commentrss.aspx?PostID=207162</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;In this section we will look at ways ADO.Net v2.0 (Code named 'Whidbey') allows us to insert/delete/update Xml data values in a table containing Xml column in SQL Server 2005 (Code named 'Yukon').&amp;nbsp; With earlier versions of the .Net Framework (1.0 and 1.1) the System.Data.SqlClient namespace will consume and emit XML as String values.Because of this, it should&amp;nbsp;not be a&amp;nbsp;surprise that they would behave the same way with Whidbey.&amp;nbsp;This was made so as&amp;nbsp;to not break customers using this&amp;nbsp;new datatype of the Server with&amp;nbsp;older versions of the framework. &lt;I&gt;Note: The examples below use the Customer table defined in the &lt;A href="http://blogs.msdn.com/sushilc/archive/2004/07/29/201042.aspx"&gt;Part I &lt;/A&gt;of the blog.&lt;/I&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;STRONG&gt;&lt;U&gt;Xml Values from DataReader:&lt;/U&gt; &lt;/STRONG&gt;XML values in SqlDataReader are surfaced like any other type. The GetValue() method will get the Xml Value as String where as the GetSqlValue() method will get the value as an instance of SqlString. The following examples show both of the retrieval methods.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;B&gt;Get Value as XmlReader&lt;BR&gt;&lt;/B&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (SqlCommand Cmd = Conn.CreateCommand())&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Cmd.CommandText= "SELECT OrderXml FROM Customer WHERE CustomerId = 2";&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SqlDataReader Reader = Cmd.ExecuteReader();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (Reader.Read()) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string XmlValue = (string) Reader.GetValue(0); //Gets the XML value as string&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;B&gt;Get Value as SqlXml&lt;/B&gt;: With ADO.Net 2.0, we have introduced a new class SqlXml. This new class adds SQL-specific feature like null semantics to the XML type. This is consistent with other SqlTypes (like SqlString for VARCHAR, SqlInt16 for Integer). This class allows the user to create an XmlReader (not to be confused with DataReader) on the SqlXml value using the SqlXml.CreateReader method. An example is shown below:&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (SqlCommand Cmd = Conn.CreateCommand())&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Cmd.CommandText= "SELECT OrderXml FROM Customer WHERE CustomerId = 2";&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;SqlDataReader Reader = Cmd.ExecuteReader();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if (Reader.Read()) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; SqlXml SqlXmlValue = Reader.GetSqlXml(0);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PrintXmlReader(SqlXmlValue.CreateReader()); //Gets the XmlReader from SqlXml object and prints it using a Helper function&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;/FONT&gt;Note:&lt;BR&gt;1. In the above examples, SqlClient Managed Provider internally converts the Binary formatted XML that it receives from the server into string representation. &lt;BR&gt;2. To get the XmlReader object for the XML data type, use the SqlXml.CreateReader method.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;B&gt;&lt;U&gt;Parameterized&lt;/U&gt; &lt;U&gt;Xml Values&lt;/U&gt;&lt;BR&gt;&lt;/B&gt;XML values can also be used in parameters. The value itself can be expressed as a string, an XmlReader-derived type instance or a SqlXml object. Examples below show how to set the parameters for each of these values.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;B&gt;Xml Value as String &lt;/B&gt;In this example, the string is directly used to set the Parameter's Value&lt;B&gt;&lt;BR&gt;&lt;/B&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp; using (SqlConnection Conn = new SqlConnection (ConnectionString))&lt;BR&gt;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Conn.Open();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (SqlCommand Cmd = Conn.CreateCommand())&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //INSERT VALUES using PARAMS &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;XmlReader XmlValue = new XmlTextReader("Sample.Xml");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Cmd.CommandText = "INSERT INTO Customer VALUES (4,'Chris Andrews',@XmlFile)";&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Cmd.Parameters.AddWithValue("@XmlFile", &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma color=#0000ff size=2&gt;"&amp;lt;order&amp;gt;&amp;lt;item&amp;gt;&amp;lt;id&amp;gt;20&amp;lt;/id&amp;gt;&amp;lt;name&amp;gt;Widgets&amp;lt;/name&amp;gt;&amp;lt;units&amp;gt;3&amp;lt;/units&amp;gt;&amp;lt;/item&amp;gt;&amp;lt;/order&amp;gt;"&lt;/FONT&gt;&lt;FONT face=Tahoma size=2&gt;&lt;FONT color=#0000ff&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Cmd.ExecuteNonQuery();&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;}&lt;/FONT&gt;&lt;BR&gt;&lt;B&gt;Xml Value as SqlXml&lt;/B&gt;&lt;BR&gt;Just like other types, SQL-specific type for XML can be used to set the Parameter's value for XML. SqlXml ctor() takes XmlReader or a Stream. The following example creates an instance of XmlTextReader and then sets the value of the parameter. An instance of XmlTextReader is created by opening a file with name:"Sample.Xml".&amp;nbsp; &lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (SqlConnection Conn = new SqlConnection (ConnectionString))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Conn.Open();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (SqlCommand Cmd = Conn.CreateCommand())&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //INSERT VALUES using PARAMS &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Cmd.CommandText = "INSERT INTO Customer VALUES (4,'Chris Andrews',@XmlFile)";&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SqlXml SqlXmlValue = new SqlXml( new XmlTextReader ("SampleInfo.Xml")); // This passes the file SampleInfo.Xml as the parameter's value&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Cmd.Parameters.AddWithValue("@XmlFile", SqlXmlValue);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Cmd.ExecuteNonQuery();&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;/FONT&gt;Note: You have to make sure that the XmlReader that is passed as an argument is on the top most node, else partial results will be inserted in the table, depending on the current position of the XmlReader.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Disclaimer: This posting is provided "AS IS" with no warranties, and confers no rights&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=207162" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/sushilc/archive/tags/SQL+Server_2700_s+Xml+DataType+w_2F00_ADO.Net+2.0/default.aspx">SQL Server's Xml DataType w/ADO.Net 2.0</category></item><item><title>XML Data type in ADO.Net v2.0 - Part I</title><link>http://blogs.msdn.com/sushilc/archive/2004/07/29/201042.aspx</link><pubDate>Thu, 29 Jul 2004 20:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:201042</guid><dc:creator>sushilc</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/sushilc/comments/201042.aspx</comments><wfw:commentRss>http://blogs.msdn.com/sushilc/commentrss.aspx?PostID=201042</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;After a long hiatus, let me start with how the new XML data type in SQL Server 2005 is exposed via ADO.Net 2.0. I will discuss this new data type in following parts:&lt;/FONT&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;XML data type in SQL Server 2005 &amp;#8211; A brief Introduction : You can consider this as a crash course on XML data type in SQL Server 2005.&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;ADO.Net 2.0 and XML DataType&lt;/FONT&gt; 
&lt;OL type=a&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Getting XML values from SqlDataReader.&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Passing XML values as Parameters&lt;/FONT&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;The following is section 1 of the 2 part series:&lt;/FONT&gt;&lt;/P&gt;&lt;B&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;XML data type in SQL Server 2005 &amp;#8211; A brief Introduction&lt;/FONT&gt;&lt;/P&gt;&lt;/B&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;A new scalar data type is introduced in SQL Server 2005 for storage and retrieval of XML data. XML value that is stored in the column is basically an XML fragment like single root, multiple roots, text nodes, empty string, text nodes at the top. Consider you want to create a Customer table with the following Columns:&lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;FONT face=Tahoma size=2&gt;CustomerId &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int, &lt;BR&gt;CustomerName &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; varchar(40), &lt;BR&gt;OrderXml &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; xml&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;FONT face=Tahoma&gt;
&lt;P dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;FONT size=2&gt;Here is an example showing how to create such a table and insert some values as literals using TSQL&lt;/FONT&gt;&lt;/P&gt;
&lt;P dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma size=2&gt;&lt;STRONG&gt;Untyped XML&lt;BR&gt;&lt;/STRONG&gt;&lt;FONT color=#0000ff&gt;CREATE TABLE Customer (CustomerId int, CustomerName varchar(40), OrderXml xml)&lt;BR&gt;go&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma color=#0000ff size=2&gt;INSERT INTO Customer VALUES (1,'Allison Gray',&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'&amp;lt;order&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;item&amp;gt;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;id&amp;gt;20&amp;lt;/id&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;name&amp;gt;Widgets&amp;lt;/name&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;units&amp;gt;3&amp;lt;/units&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/item&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/order&amp;gt;')&lt;BR&gt;go&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;EM&gt;&lt;FONT size=1&gt;&lt;FONT size=2&gt;Fact: If the string character is Unicode then the XML values is always parsed in as UTF-16.&lt;/FONT&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/EM&gt;&amp;nbsp;&lt;BR&gt;&lt;STRONG&gt;Typed XML&lt;BR&gt;&lt;/STRONG&gt;The above XML data type is untyped meaning it is not associated with any schema. If we know that OrderXML adheres to one specific XML Schema, we can associate it to a previously loaded XML schema with the following syntax:&lt;BR&gt;&lt;FONT color=#0000ff&gt;-- Consider the Schema/namespace - OrderSchema is defined using the CREATE XML SCHEMA COLLECTION statement&lt;BR&gt;CREATE TABLE Customer (CustomerId int, CustomerName varchar(40), OrderXml xml(OrderSchema))&lt;BR&gt;go&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;STRONG&gt;XML Methods&lt;/STRONG&gt;&lt;BR&gt;In addition to the above, XML data type has some methods. Some of them are:&lt;/FONT&gt;&lt;/P&gt;&lt;FONT face=Tahoma size=2&gt;
&lt;OL&gt;
&lt;LI&gt;Query method: Given a XQUERY as an argument, this method performs that query on the XML data type. 
&lt;LI&gt;Value method: Given a XQUERY as an argument, this method runs the query and returns a scalar value. 
&lt;LI&gt;Exist method: Given an XQUERY as an argument, this method returns 1 if the XQuery expression returns a non-empty result from the XML column else it returns 1. 
&lt;LI&gt;Modify method: Used to modify XML data. Basically this performs XML data manipulation as specified in the arguments.&lt;/FONT&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Since our goal is to see the interaction of XML data type with ADO.Net, the above introduction would suffice. There are other awesome features on XML data type on the server and are not discussed here due to the scope of the article.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Disclaimer: This posting is provided "AS IS" with no warranties, and confers no rights&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=201042" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/sushilc/archive/tags/SQL+Server_2700_s+Xml+DataType+w_2F00_ADO.Net+2.0/default.aspx">SQL Server's Xml DataType w/ADO.Net 2.0</category></item></channel></rss>