XML document verification-Programming

Source: Internet
Author: User
1. MSXML parser and. net Program Set support for XML Verification

The XML specification not only describes the XML data format and syntax, but also specifies a two-layer user architecture for processing XML data. The first layer is the XML Parser (also known as the XML handler ). The XML Parser checks an XML document to ensure that the document is in the correct format. When an XML document has a related DTD, XDR, or schema, the parser must also ensure that the document is valid. The behavior of the XML Parser can be defined as an attempt to reduce the burden on the application to process XML data. At the same time, the content and structure of XML data are delivered to the second layer (XML application layer) in a specified manner ).

Msxml3 is a commonly used parser. The msxml3 parser executes all the checks mentioned above. If you have used msxml3, you may already be familiar with using the validateonparse attribute or the validate () method to verify XML documents based on DTD or XDR schema. With the validateonparse attribute, a document is verified when it is parsed into a DOM structure, while the validate () method allows runtime verification for a document that has been parsed and loaded.

For Asp.net, Microsoft has constructed a brand new assembly-based parser to replace msxml3. implementation in the XML namespace.. NET Framework. the XML set is associated. To use the XML function of the. NET Framework, you only need to reference the system. xml namespace in the project.

Asp.net relies on the system. xml set for XML parsing. The rich classes, interfaces, and other objects in the program set, as well as their methods and attributes, provide powerful support for XML operations and related technologies (specifications. Use.. Net supports multiple programming languages (Visual C #, VB. and can easily perform xmldr schema or W3C XML Schema verification on XML documents. In addition, the verification method becomes more powerful and flexible ..

Although you can still call msxml3 In the. NET language, this will not give full play to the powerful features of. net.

Ii. Use xmlvalidatingreader to read XML documents

The. NET assembly system. xml contains many classes that provide XML functionality on the. NET platform. Xmlvalidatingreader class (an implementation of the xmlreader class) is one of them. This class provides verification support when you read XML documents or XML fragments into the system. It implements the validity constraints defined by the DTD, XML data simplified (XDR) architecture, XML Architecture Definition Language (XSD) architecture, and other specifications.

1. Construct an xmlvalidatingreade class object instance

There are multiple methods to initialize the new instance of the xmlvalidatingreader class. The most common method is to pass in the xmlreader type parameters:

Public xmlvalidatingreader (xmlreader reader );
Xmlvalidatingreader vreader = new xmlvalidatingreader (xmltextreader XTR );

As one of the specific implementations of xmlreader, The xmltextreader class provides fast, forward-only, and cache-free reading of XML documents, while xmlvalidatingreader can use all the content returned from xmltextreader, and further provide verification support. Of course, if everything works normally, this process will not cause information loss, and all nodes and attributes returned from the given xmlreader will also be returned from the verification reader. New nodes not returned from the base reader may be added to this reader (for example, default attributes and sublevels of object references ).

2. Specify the verification type

As you can see earlier, there are three specifications used to verify the XML document. They are DTD, XDR, and XMLSCHEMA. Therefore, before performing the verification, You need to determine the verification type, which is completed by setting the validationtype attribute of the validatingreader class:

Vreader. validationtype = validationtype. schema.

This lineCodeDeclare the verification as XSD Schema.

3. Use xmlschemacollection class Cache architecture

If you need to verify against XDR or XSD Schema, you can use the xmlschemacollection class to cache the architecture, which will improve the performance. The add method of the xmlschemacollection class loads the schema, And the schema is associated with the namespace URI. For the source file "XML schema" (. XSD), this is usually the targetnamespace attribute of the schema.

Xmlschemacollection xsc = new xmlschemacollection ();
Xsc. Add ("http://www.tuha.net";, "vschema. XSD ");

Of course, this is not required if the architecture is inline in the XML document.

4. Associated architecture Cache

After adding a schema to xmlschemacollection, xmlvalidatingreader cannot automatically identify and use the schema. This process is completed by referencing the schema file cached in xmlschemacollection using the reader's schemas attribute:

Vreader. schemas. Add (xsc );

5. validationeventhandler event handler callback

An error may occur when validatingreader is used to read the XML document for verification. In this case, you can report verification errors and warnings through the validationeventhandler callback. Validationeventhandler events are used to set an event handler to receive information about Document Type Definitions (DTD), simplified XML (XDR), and XML Schema Definition Language (XSD) Schema validation errors.

However, if validationeventhandler is not provided, you can still use a general exception processor to capture errors. When an analysis error occurs, the xmlexception is reported. If a verification error occurs, xmlschemaexception is thrown. Of course, xmlvalidatingreader cannot be restarted due to any exception.

The specified event and callback follow the common practice: connect xmlvalidatingreader to the event handler validationeventhandler through + =:

Vreader. validationeventhandler + = new validationeventhandler (vcallback );

The parameter vcallback is the method name of the callback handler. This method must contain a validationeventargs type parameter. The validationeventargs class has attributes for the following items: text messages, indicating the xmlseveritytype enumeration of error or warning, and an exception that contains the xmlschemaexception information associated with a specific verification error.

Private void vcallback (Object sender, validationeventargs ARGs)
// Processing code when an error occurs

This step is not necessary. If you can ensure that errors do not occur or happen, proceed!

6. Perform read Verification

After completing the above preparations, you can use the read method of the xmlvalidatingreader class to verify the reading of the XML document. It can be any of the read, readinnerxml, readouterxml methods, and other methods that will change the contact, such as the SKIP () method. At this time, verification will occur.

While (vreader. Read ())
// Process read content

Iii. Instances

Based on the above knowledge, we will create a Windows console application to process product data in the business field. Generally, the product data of different companies will follow a certain format.

XSD, which provides structure information for XML documents and follows consistent standards during data exchange.

<? XML version = "1.0" encoding = "UTF-8"?>
<Xs: schema id = "Products" targetnamespace = "http://www.tuha.net"; elementformdefault = "qualified"
Xmlns = "http://www.tuha.net"; xmlns: mstns = "http://www.tuha.net ";
Xmlns: xs = "http://www.w3.org/2001/XMLSchema";>
<Xs: element name = "Products" type = "A1"> </Xs: Element>
<Xs: complextype name = "B1">
<Xs: sequence>
<Xs: element name = "name" type = "XS: string"/>
<Xs: element name = "type" type = "XS: string"/>
<Xs: element name = "usefor" type = "XS: string"/>
</Xs: sequence>
</Xs: complextype>
<Xs: complextype name = "A1">
<Xs: sequence maxoccurs = "unbounded">
<Xs: element name = "item" type = "B1"/>
</Xs: sequence>
</Xs: complextype>
</Xs: schema>

According to the above architecture file, a product data XML document is constructed below. Some sections of the product library content are used here. To facilitate testing, it becomes a complete XML document:

<? XML version = "1.0" encoding = "UTF-8"?>
<Products xmlns = "http://www.tuha.net";>
<Name> talking online </Name>
<Type> fitbench </type>
<Usefor> communicate </usefor>
<Name> debugging online </Name>
<Type> professional </type>
<Usefor> machine </usefor>

The following application processes the XML document and verifies whether the data is valid in the architecture!

Using system;
Using system. IO;
Using system. xml;
Using system. xml. Schema;
Namespace myxmlvalidationgreader
Class class1
Static bool Sign = true;
Static void main (string [] ARGs)
Xmltextreader XTR = NULL;
Xmlvalidatingreader xvr = NULL;
String xmlfile = "../products. xml"; // XML Source Document
String xsdfile = ".../../products. XSD"; // XSD architecture document
XTR = new xmltextreader (xmlfile); // construct a non-verified Reader
Xmlschemacollection xsc = new xmlschemacollection (); // construct schema Cache
Xsc. Add ("http://www.tuha.net";, xsdfile); // Add the schema file and the corresponding namespace in the cache
Xvr = new xmlvalidatingreader (XTR); // construct a verification Reader
Xvr. schemas. Add (xsc); // associate the reader with the schema set
Xvr. validationtype = validationtype. Schema; // you can specify schema as the verification type.
Xvr. validationeventhandler + = new validationeventhandler (vcallback );
// Event handler when an error occurs
While (xvr. Read () // executes the read operation
Console. Write ("finished! "+ Sign. tostring ());
Private Static void vcallback (Object sender, validationeventargs ARGs)
// Error callback Program
Sign = false; // change the flag

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.