Leverage the. NET schema object model to improve flexibility

Source: Internet
Author: User
Tags definition data structures include net xml object model serialization xml reader visual studio
Object


XML Schema is the recommended choice for the consortium, which provides tools for defining the structure, content, and semantics of XML files. XML schemas have two key advantages over the other two schema modeling tool file type definitions (DTDs) and XDR (XML Data reduced). First, because it is an official recommendation of the Consortium for the definition of XML data structures, the same definition is used by corporate organizations. Second, it is the latest schema technology, so its construction is to fix the vulnerabilities and flaws in the other two, especially in the DTD.

XDR is not an alternative to schema technology because it is just an early draft of Microsoft's XML Schema specification. In. NET, XDR is primarily supported for backward compatibility. SQL Server 2000 's XML extensions and Microsoft's COM parser (MSXML) are still widely used.

Let's start by looking at how the XML Schema Object model is made available to you. NET class to manipulate the schema part. Then, let's take a look at several ways to deal with schema information.

Schema background
XML schemas represent XML type systems and should be used to describe classes and objects when they serialize their state to other applications and platforms. The. NET XML Schema Object Model (SOM) can help in. Building a bridge between the Net-specific type system and the XML Schema type system makes it easy to create and modify schemas in a planned way. The schema file is an XML file with an. xsd extension.

All of the data types that can be used in an XML schema file have one. NET copy. Once an XSD schema is compiled as. NET presentation (representation) object model, you can use the SOM class to access them. The schema compiler assembles XSD into a XmlSchema object that displays information about the schema through its properties and methods.

On a given platform, an effective serialization mechanism between XSD and a synthetic binary class provides tremendous potential, which is a critical step towards full cross-platform interoperability implementation. In. NET, XML serialization is accomplished by XmlSerializer classes and leveraging the XML Schema Definition tool (Xsd.exe) service. This tool is a binary executable in the. NET Framework SDK, which you will find in the bin subdirectory of the. NET Framework installation path, which is typically C:\Program Files\Microsoft Visual Studio . net\frameworksdk.

Xsd.exe can generate a C # or Visual Basic class from an XSD file and infer an XML schema from an XML source file. This tool is also responsible for visual Studio. NET executes a schema-related technology in XML.

Check som

The. NET Framework provides a class hierarchy to edit an existing schema or create a new schema from scratch. The class is defined in the System.Xml.Schema namespace, and the root class in the namespace is XmlSchema. Once the application retains an instance of a class, they can load an existing XSD file and use the contained information to form internal properties and collections, and by using the XmlSchema programming interface, you can add or edit elements, attributes, and other schema parts. Finally, the class provides a write method that allows you to save current content for a valid stream object.

There are two ways to create an instance of a XmlSchema class: You can use the default constructor, it will return an instance of a new empty class, or you can use the static Read method.

The Read method runs through a stream, a text reader, or an XML reader on the available schema information, and the returned schema has not been compiled. The Read method accepts a second argument, which is a validation event handler. You can set this argument to NULL, but you cannot find and handle validation errors in the read schema. Table A shows how to use the. NET som to read and compile a schema.

Once the schema is compiled, you can access the constituent elements of the schema defined by the Post-Schema validation Information group, and you need to use the SchemaTypes collection to access the actual types in the schema.

One of the differences between the information before and after compilation is that a synthetic type that is contained (not previously defined) will be detected only after the schema has been compiled. For example, if you use the <xs:include> tag to introduce an external type definition, to systematically find the existence of a type, you must first compile the schema. The process expands the <include> declaration and adds it to the type definition.

The Code in Table B shows how to get a list of the composite types defined in the specified schema after compilation.

Once a schema is read to memory, you are free to manipulate its structure, while indirect tags, such as <include> and <IMPORT>, are recognized as individuals or individual objects. In other words, they represent themselves rather than what they want to include or introduce.

Application software and embedded schema

Schema information is the basis for the client application to understand the structure of the XML data they get from the server. However, schema information is also an additional burden, especially in distributed applications where it takes up a portion of the bandwidth.

In some cases, you can treat schemas like the debugging information in a Windows executable: It is essential to the development of your application, and it is useless once the application is released. This pattern does not apply to all applications, but it forms an interesting form of optimization. How can the format of XML data be exchanged in different ways once two communication modules are made for one XML format and hard-coded in the software?

When the XML file is not fully controlled by the relevant application software, the validation of the schema is no longer an optional function. The first way to think about it is to have the client application store the schema locally and load it when it needs to validate the imported file. For. NET application, the Xmlschema.read static method is what you need to load an existing schema file. Another method needs to dynamically create and compile a schema object and then use it to validate the file. XML validation also provides another way to ensure that the file is in the desired form.

Conclusion
With XML schemas, you can describe the layout of a file in a very strict form, so that users don't have to dream about it. As we've seen here, the XML schema Object model enables you to take advantage of schema support in the. NET framework.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.