Export (0) Print
Expand All

Using XML Schemas

XML programming in SMO is limited to providing XML data types, XML namespaces, and simple indexing on XML data type columns.

Microsoft SQL Server provides native storage for XML document instances. XML schemas let you define complex XML data types, which can be used to validate XML documents to ensure data integrity. The XML schema is defined in the XmlSchemaCollection object.

To use any code example that is provided, you will have to choose the programming environment, the programming template, and the programming language in which to create your application. For more information, see How to: Create a Visual Basic SMO Project in Visual Studio .NET or How to: Create a Visual C# SMO Project in Visual Studio .NET.

This code example shows how to create an XML schema by using the XmlSchemaCollection object. The Text property, which defines the XML schema collection, contains several double quotation marks. These are replaced by the chr(34) string.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server()
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Define an XmlSchemaCollection object by supplying the parent database and name arguments in the constructor.
Dim xsc As XmlSchemaCollection
xsc = New XmlSchemaCollection(db, "MySampleCollection")
xsc.Text = "<schema xmlns=" + Chr(34) + "http://www.w3.org/2001/XMLSchema" + Chr(34) + "  xmlns:ns=" + Chr(34) + "http://ns" + Chr(34) + "><element name=" + Chr(34) + "e" + Chr(34) + " type=" + Chr(34) + "dateTime" + Chr(34) + "/></schema>"
'Create the XML schema collection on the instance of SQL Server.
xsc.Create()


This code example shows how to create an XML schema by using the XmlSchemaCollection object. The Text property, which defines the XML schema collection, contains several double quotation marks. These are replaced by the chr(34) string.

//Connect to the local, default instance of SQL Server. 

{ 
   Server srv = default(Server); 
   srv = new Server(); 
   //Reference the AdventureWorks database. 
   Database db = default(Database); 
   db = srv.Databases("AdventureWorks"); 
   //Define an XmlSchemaCollection object by supplying the parent
   // database and name arguments in the constructor. 
   XmlSchemaCollection xsc = default(XmlSchemaCollection); 
   xsc = new XmlSchemaCollection(db, "MySampleCollection"); 
   xsc.Text = "<schema xmlns=" + Strings.Chr(34) + "http://www.w3.org/2001/XMLSchema" + Strings.Chr(34) + " xmlns:ns=" + Strings.Chr(34) + "http://ns" + Strings.Chr(34) + "><element name=" + Strings.Chr(34) + "e" + Strings.Chr(34) + " type=" + Strings.Chr(34) + "dateTime" + Strings.Chr(34) + "/></schema>"; 
   //Create the XML schema collection on the instance of SQL Server. 
   xsc.Create(); 
} 

Community Additions

ADD
Show:
© 2014 Microsoft