Scheming for Schema

As many of you know, we already provide access to the schema for the healthvault types through the Type Schema Browser and the Raw XML API reference page.

If those pages don't do exactly what you want - say, you want to look at all the schemas together in a tool - you can access the schemas programmatically. The schema for a data type is built on some lower-level schemas (or types, if you wish):

The first one can be fetched programmatically, and the second and third you will need to download directly from the links I provided.

Access to the schema is provided through the ItemTypeManager class. You can call GetBaseHealthRecordItemTypeDefinition() to get the base schema (base.xsd), and then use GetHealthRecordItemTypeDefinition() to get the xsd for a specific type.

And, you can get a list of all the types from the "thing-types" vocabulary, thusly:

Vocabulary thingTypes = ApplicationConnection.GetVocabulary("thing-types");

Here's a bit of code that I wrote to fetch all of the schemas and store them in separate files:

     protected void Page_Load(object sender, EventArgs e)
    {
        HealthRecordItemTypeDefinition baseTypes = ItemTypeManager.GetBaseHealthRecordItemTypeDefinition(ApplicationConnection);
        SaveTypeXSD(baseTypes, "base");
         Vocabulary thingTypes = ApplicationConnection.GetVocabulary("thing-types");
         foreach (KeyValuePair<string, VocabularyItem> item in thingTypes)
        {
            Guid thingType = new Guid(item.Key);
            HealthRecordItemTypeDefinition definition = ItemTypeManager.GetHealthRecordItemTypeDefinition(thingType, ApplicationConnection);
            SaveTypeXSD(definition, definition.Name);
        }
         int k = 12;
    }
     void SaveTypeXSD(HealthRecordItemTypeDefinition definition, string name)
    {
        string directoryName = MapPath("platform");
        Directory.CreateDirectory(directoryName).CreateSubdirectory("web").CreateSubdirectory("xsd");
         string filename = MapPath(@"platform\web\xsd\" + name + ".xsd");
         using (StreamWriter writer = System.IO.File.CreateText(filename))
        {
            string schema = definition.XmlSchemaDefinition;
            writer.Write(schema);
        }
    }
 Note that it puts the schema definitions in a directory named platform\web\xsd. This is the same structure that the schemas use in our source tree, and this makes sure things work correctly