13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

For an effective programmatic manipulation of an <strong>XML</strong> schema, you need an objectmodel. An object model enables you to build and edit schema <strong>in</strong><strong>for</strong>mation <strong>in</strong> memory. Italso gives you access to each element that <strong>for</strong>ms the schema and that exposesread/write properties <strong>in</strong> homage to the pre-schema-validation and post-schemavalidation<strong>in</strong>foset specifications.The .<strong>NET</strong> Framework provides a hierarchy of classes under the System.Xml.Schemanamespace to edit exist<strong>in</strong>g schemas or create new ones from the ground up. The rootclass of the hierarchy is XmlSchema. Once your application holds an <strong>in</strong>stance of theclass, it can load an exist<strong>in</strong>g XSD file and populate the <strong>in</strong>ternal properties andcollections with the conta<strong>in</strong>ed <strong>in</strong><strong>for</strong>mation. By us<strong>in</strong>g the XmlSchema programm<strong>in</strong>g<strong>in</strong>terface, you can then add or edit elements, attributes, and other schema components.F<strong>in</strong>ally, the class exposes a Write method that allows you to persist to a valid streamobject the current contents of the schema.Read<strong>in</strong>g a Schema from a FileYou can create an <strong>in</strong>stance of the XmlSchema class <strong>in</strong> two ways. You can use thedefault constructor, which returns a new, empty <strong>in</strong>stance of the class, or you can usethe static Read method.The Read method operates on schema <strong>in</strong><strong>for</strong>mation available through a stream, a textreader, or an <strong>XML</strong> reader. The schema returned is not yet compiled. The Read methodaccepts a second argument—a validation event handler such as the ones discussed <strong>in</strong>the section "The XmlValidat<strong>in</strong>gReader <strong>Programm<strong>in</strong>g</strong> Interface," on page 78. You canset this argument to null, but <strong>in</strong> this case you won't be able to catch and handlevalidation errors. The follow<strong>in</strong>g code shows how to read and compile a schema us<strong>in</strong>gthe .<strong>NET</strong> SOM:XmlSchema schema;XmlTextReader reader = new XmlTextReader(filename);schema = XmlSchema.Read(reader, null);schema.Compile(null);//// Do someth<strong>in</strong>g here//⋮reader.Close();Once the schema has been compiled, you can access the constituent elements of theschema as def<strong>in</strong>ed by the PSVI. To access the actual types <strong>in</strong> the schema, you use theSchemaTypes collection. One of the differences between the <strong>in</strong><strong>for</strong>mation availablebe<strong>for</strong>e and after compilation is that an <strong>in</strong>cluded complex type will not be detected untilthe schema is compiled. For example, <strong>in</strong> eu_address.xsd, we extended theAddressType type after import<strong>in</strong>g it through the tag. To programmaticallydetect the presence of the AddressType complex type, however, you must first compilethe schema, which would expand the <strong>in</strong>clude element that imports the type def<strong>in</strong>ition.The follow<strong>in</strong>g code snippet demonstrates how to get the list of complex types def<strong>in</strong>ed <strong>in</strong>the specified schema after compilation:void ListComplexTypes(str<strong>in</strong>g filename){XmlSchema schema;99

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!