|
| 1 | +--- |
| 2 | +title: SQL Projects XML Schema Collections |
| 3 | +description: "Automatic generation of XML schema collections in SQL database projects." |
| 4 | +author: dzsquared |
| 5 | +ms.author: drskwier |
| 6 | +ms.reviewer: maghan, randolphwest |
| 7 | +ms.date: 03/03/2026 |
| 8 | +ms.service: sql |
| 9 | +ms.subservice: sql-database-projects |
| 10 | +ms.topic: concept-article |
| 11 | +ai-usage: ai-assisted |
| 12 | +ms.collection: |
| 13 | + - data-tools |
| 14 | +--- |
| 15 | + |
| 16 | +# XML schema collections in SQL projects |
| 17 | + |
| 18 | +[!INCLUDE [SQL Server Azure SQL Database Azure SQL Managed Instance FabricSQLDB](../../../includes/applies-to-version/sql-asdb-asdbmi-fabricsqldb.md)] |
| 19 | + |
| 20 | +SQL database projects support automatic generation of XML schema collection objects from XSD (XML Schema Definition) files. When you include an XSD file in your project with the appropriate metadata, the build process creates a `CREATE XML SCHEMA COLLECTION` statement that you can use to enforce XML data validation in your database. |
| 21 | + |
| 22 | +XML schema collections provide typed XML columns in SQL Server, enabling the database engine to validate XML data against defined schemas and optimize query performance. |
| 23 | + |
| 24 | +## Configure XSD files in the project |
| 25 | + |
| 26 | +To generate an XML schema collection from an XSD file, add a `Build` item to your project file (`.sqlproj`) with two required metadata elements: |
| 27 | + |
| 28 | +- **RelationalSchema**: The database schema where the XML schema collection is created (for example, `dbo`) |
| 29 | +- **XMLSchemaCollectionName**: The name of the resulting XML schema collection object in the database |
| 30 | + |
| 31 | +The following example shows how to configure an XSD file in a SQL project: |
| 32 | + |
| 33 | +```xml |
| 34 | +<?xml version="1.0" encoding="utf-8"?> |
| 35 | +<Project DefaultTargets="Build"> |
| 36 | + <Sdk Name="Microsoft.Build.Sql" Version="2.1.0" /> |
| 37 | + <PropertyGroup> |
| 38 | + <Name>MyDatabase</Name> |
| 39 | + <DSP>Microsoft.Data.Tools.Schema.Sql.Sql170DatabaseSchemaProvider</DSP> |
| 40 | + <ModelCollation>1033, CI</ModelCollation> |
| 41 | + </PropertyGroup> |
| 42 | + <ItemGroup> |
| 43 | + <Build Include="OrderSchema.xsd"> |
| 44 | + <RelationalSchema>dbo</RelationalSchema> |
| 45 | + <XMLSchemaCollectionName>OrderSchemaCollection</XMLSchemaCollectionName> |
| 46 | + </Build> |
| 47 | + </ItemGroup> |
| 48 | +</Project> |
| 49 | +``` |
| 50 | + |
| 51 | +## XSD file example |
| 52 | + |
| 53 | +The XSD file defines the structure that XML data must follow. The following example defines a schema for an `Order` element containing one or more `Item` elements: |
| 54 | + |
| 55 | +```xml |
| 56 | +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> |
| 57 | + <xs:element name="Order"> |
| 58 | + <xs:complexType> |
| 59 | + <xs:sequence> |
| 60 | + <xs:element name="Item" type="xs:string" maxOccurs="unbounded"/> |
| 61 | + </xs:sequence> |
| 62 | + </xs:complexType> |
| 63 | + </xs:element> |
| 64 | +</xs:schema> |
| 65 | +``` |
| 66 | + |
| 67 | +When the project builds, this XSD file generates a `CREATE XML SCHEMA COLLECTION` statement that creates the `OrderSchemaCollection` object in the `dbo` schema. |
| 68 | + |
| 69 | +## Use typed XML columns |
| 70 | + |
| 71 | +After the XML schema collection is defined, reference it in table definitions to create typed XML columns. Typed XML columns validate data against the schema and improve query performance. |
| 72 | + |
| 73 | +The following example creates a table with a typed XML column that uses the generated schema collection: |
| 74 | + |
| 75 | +```sql |
| 76 | +CREATE TABLE dbo.Orders ( |
| 77 | + Id INT PRIMARY KEY, |
| 78 | + OrderData XML(dbo.OrderSchemaCollection) |
| 79 | +); |
| 80 | +``` |
| 81 | + |
| 82 | +When you insert data into the `OrderData` column, SQL Server validates the XML against the `OrderSchemaCollection` schema. Invalid XML that doesn't conform to the schema is rejected. |
| 83 | + |
| 84 | +## Related content |
| 85 | + |
| 86 | +- [XML schema collections (SQL Server)](../../../relational-databases/xml/xml-schema-collections-sql-server.md) |
| 87 | +- [XML data (SQL Server)](../../../relational-databases/xml/xml-data-sql-server.md) |
| 88 | +- [Project properties](project-properties.md) |
0 commit comments