Creating XSD’s

February 27, 2009 § 1 Comment

XSD’s are nice, but they’re kind of a pain to define. You wind up having to look up the syntax every time you need to create one, because you don’t do it often enough to remember it when you need (or at least, I don’t remember it). Well, I had to define an XSD recently, and I thought that maybe there’s a better way to handle it.

I find it much easier to create an XML document than to create an XSD, and frequently, I even already have an XML document. So here is a little function for your toolbox that helps a ton with that. Just pass it the filename to your XML file, and give it another filename to write the XSD to.

public XmlDocument CreateXSD(string xmlFileName, string xsdFileName)
{
    XmlDocument xml = new XmlDocument();
    xml.Load(xmlFileName);
 
    XmlSchemaInference inference = new XmlSchemaInference();
    MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(xml.InnerXml));
    XmlTextReader reader = new XmlTextReader(stream);
 
    XmlSchemaSet schemaSet = inference.InferSchema(reader);
    foreach (XmlSchema schema in schemaSet.Schemas())
    {
        TextWriter target = new StringWriter();
        schema.Write(target);
        target.Close();
 
        StreamWriter sw = new StreamWriter(xsdFileName);
        sw.Write(target.ToString());
        sw.Close();
    }
    return xml;
}

So if we take a sample XML file (I used this one on cafeonleche.org), we get an XSD that looks like this:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="SEASON">
    <xs:complexType mixed="true">
      <xs:sequence>
        <xs:element name="YEAR" type="xs:unsignedShort" />
        <xs:element maxOccurs="unbounded" name="LEAGUE">
          <xs:complexType mixed="true">
            <xs:sequence>
              <xs:element name="LEAGUE_NAME" type="xs:string" />
              <xs:element maxOccurs="unbounded" name="DIVISION">
                <xs:complexType mixed="true">
                  <xs:sequence>
                    <xs:element name="DIVISION_NAME" type="xs:string" />
                    <xs:element maxOccurs="unbounded" name="TEAM">
                      <xs:complexType mixed="true">
                        <xs:sequence>
                          <xs:element name="TEAM_CITY" type="xs:string" />
                          <xs:element name="TEAM_NAME" type="xs:string" />
                          <xs:element minOccurs="0" maxOccurs="unbounded" name="PLAYER">
                            <xs:complexType>
                              <xs:sequence>
                                <xs:element name="SURNAME" type="xs:string" />
                                <xs:element name="GIVEN_NAME" type="xs:string" />
                                <xs:element name="POSITION" type="xs:string" />
                                <xs:element minOccurs="0" name="WINS" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="LOSSES" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="SAVES" type="xs:unsignedByte" />
                                <xs:element name="GAMES" type="xs:unsignedByte" />
                                <xs:element name="GAMES_STARTED" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="COMPLETE_GAMES" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="SHUT_OUTS" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="ERA" type="xs:decimal" />
                                <xs:element minOccurs="0" name="INNINGS" type="xs:decimal" />
                                <xs:element minOccurs="0" name="HITS_AGAINST" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="HOME_RUNS_AGAINST" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="RUNS_AGAINST" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="EARNED_RUNS" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="HIT_BATTER" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="WILD_PITCHES" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="BALK" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="WALKED_BATTER" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="STRUCK_OUT_BATTER" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="AT_BATS" type="xs:unsignedShort" />
                                <xs:element minOccurs="0" name="RUNS" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="HITS" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="DOUBLES" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="TRIPLES" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="HOME_RUNS" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="RBI" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="STEALS" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="CAUGHT_STEALING" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="SACRIFICE_HITS" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="SACRIFICE_FLIES" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="ERRORS" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="WALKS" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="STRUCK_OUT" type="xs:unsignedByte" />
                                <xs:element minOccurs="0" name="HIT_BY_PITCH" type="xs:unsignedByte" />
                              </xs:sequence>
                            </xs:complexType>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Neat, huh?

Advertisements

Tagged: , ,

§ One Response to Creating XSD’s

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Creating XSD’s at Mike Vallotton's Blog.

meta

%d bloggers like this: