Saving A Schema

The NPCL API provides two classes for saving a schema. To save a schema to an NPCL XML file, use the class NetworkedPlanet.Npcl.XmlSchemaWriter. To write a schema to a topic map stored in a TMCore server, you can use the class NetworkedPlanet.Npcl.TopicMapSchemaWriter.

The XmlSchemaWriter is simply initialised with the XmlWriter that the output is to be written to. Some control over how the schema is written is provided by the properties NpclPrefix which sets the prefix to be used for the NPCL XML Namspace; and WriteAsFragment which controls whether the schema is written as a full XML document or as a fragment (with no XML declaration).

Example 12.2. Writing a Schema as XML

FileInfo outputFile = new FileInfo("myschema.xml");
Stream output = outputFile.Open(FileMode.Create, FileAccess.Write);
XmlTextWriter xmlWriter = new XmlTextWriter(output, System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
// Create a new XmlSchemaWriter to write as a full XML document 
// using the prefix "npcl" for the NPCL XML Namespace.
XmlSchemaWriter schemaWriter = new XmlSchemaWriter(xmlWriter, false, "npcl");
schemaWriter.WriteSchema(originalSchema);
// NOTE: The XmlSchemaWriter does not close the XmlWriter at the end of the document.
xmlWriter.Close();

The TopicMapSchemaWriter class writes the schema information into an ITopicMap instance, storing the schema information in the TMCore database as topics and associations in the topic map. The TopicMapSchemaWriter can operate in one of three different modes.

ReplaceAll

In this mode all existing schema information in the topic map is removed and replaced by the schema being written.

ReplaceExisting

In this mode existing type and constraint information in the topic map is overwritten by the input schema, but types and constraints not specified in the input schema are left in the topic map.

UpdateExisting

In this mode existing types and constraints are updated, adding any new information contained in the input schema without removing existing constraints contained in the topic map.

Example 12.3. Writing A Schema Into A Topic Map

TopicMapSchemaWriter writer = new TopicMapSchemaWriter(tm, SchemaWriterMode.ReplaceAll);
writer.WriteSchema(originalSchema);