I was wondering how I might take a table schema in SQL Server and generate an XML document from it. The ideal would be if I passed in my database name (“SalesOrders”) and an XML doc comes back reading something like:
<table=”SalesOrders”>
<columns>
<name=”SalesOrderID”/>
<datatype=”int”/>
<allowNulls=”false”/>
</name>
<name=”DateOfSale”>
<datatype=”DateTime”/>
<allowNulls=”false”/>
</name>
</columns>
</table>
You get the idea. Something along these lines, an XSD schema would be fine too. In the back of my head I think SQL Server has mechanisms for doing this but I’m not positive. Many thanks for your suggestions.
Something like the following would work. Also, note that your example XML is not well formed. I took the liberty of making it well formed.
or
Both queries would produce the following:
As a side note:
Although it’s usually a matter of taste when deciding on elements vs attributes in an XML structure, I would make
dataTypeandallowNullsattribtes as opposed to elements, which seems more intuitive to me. So, the XML structure would look something like this:The above queries can be easily modified to reflect this change.