CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

David Hayden [MVP C#]

         .NET Tutorials, Patterns, and Practices

Creating XML Schema from XML Data

When developing in Visual Studio, sometimes I forget where the features in Visual Studio end and where the features in my add-ins, like ReSharper, CodeRush, etc, begin. Sometimes I will think Visual Studio can do something and it turns out it can't - that feature is there because of an add-in.

The opposite happened to me today. Today I was working with the new XML Data Type in SQL Server 2005 when I needed to create a schema from some XML Data. Now normally this is a 2-second operation with the help of my 3rd party XML tool, but this isn't installed on my new laptop yet and there was no way I was going to leave my perch on the lanai to install it :)

After a little searching, I realized that Visual Studio has the ability to create XML Schema from XML Data and I am probably the last developer to realize this.

If you create an XML file in Visual Studio 2005 and populate it with some sample data as such:

 

<Chapters>
    <Chapter>
        <Title>Chapter 1</Title>
        <Page>1</Page>
    </Chapter>
    <Chapter>
        <Title>Chapter 2</Title>
        <Page>40</Page>
    </Chapter>
</Chapters>

 

There is the handy, dandy XML Menu with an option to create a schema from it:

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified"
        elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Chapters">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="Chapter">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Title" type="xs:string" />
                            <xs:element name="Page" type="xs:int" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

 

It appears this could be done in VS 2003 as well, which means I really need to take a break from some of my tools so I can learn all the capabilities of Visual Studio.



Comments

Jason Haley said:

# April 12, 2006 8:26 AM

Joshua Flanagan said:

I think this was actually easier in 2003 (or I just haven't learned how to do it correctly in 05).
It used to create the new schema, and add a reference to the schema in your data file, and add the schema to your project.
Now it just creates the schema... but adds no direct reference to it, and surprisingly does NOT add it to your project (its not clear where the file lives).
Also, you used to be able to right-click a schema and choose "Generate DataSet", which would do the equivalent of running xsd.exe to generate strongly typed classes for your schema. I still have yet to figure out how to do that in 2005 (without dropping down to xsd.exe)
# April 12, 2006 9:45 AM

johnwood said:

You can also do this with the xsd.exe command line tool that comes with the .Net SDK.
# April 12, 2006 1:58 PM

tod said:

VS.NET 2003 came with the xsd.exe utility that will do this.  I don't know if it's available via the GUI.

http://spaces.msn.com/justanotherse/blog/cns!9DF729B45868FBB4!1050.entry
# April 12, 2006 3:05 PM

David Hayden [MVP C#] said:

This is an example I presented at the last Sarasota .NET Developer Meeting during my SQL Server 2005...
# April 21, 2006 12:08 PM
Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News

CodeBetter.Com Home