Search code examples
.netdatabasesql-server-2005xsdndbunit

Getting XML Schema from MS SQL Database


Is it possible to generate a XML Schema of a Database programatically with .Net and C#? I want to look into NDbUnit but for big databases it would not really be feasible to make a Schema manually?


Solution

  • Is it possible to generate a XML Schema from a Database?

    It sure is, XMLSpy can generate XML Schema from a database.

    There's another way, though I've never tested it:

    create table Person
    (
    Age int not NULL check( Age > 0) ,
    Height numeric(10,2) not NULL check( Height > 5),
    Gender varchar(5) not null check( Gender in ('M', 'F', 'O')),
    BirthDate datetime null,
    )
    
    DECLARE @schema xml
    SET @schema = (SELECT * FROM Person FOR XML AUTO, ELEMENTS, XMLSCHEMA('PersonSchema'))
    select @schema