Easy XML data processing in. NET Framework (3)

Source: Internet
Author: User
Tags xml reader

Easy XML data processing in. NET Framework (3)
Reader with verification

Xmlvalidatingreader class implements the xmlreader class, which supports multiple types of XML verification: DTD, XML-data CED (XDR) architecture, and XSD, DTD, and XSD are officially recommended by W3C. XDR is a format that Microsoft used to process XML architectures in the early days.

You can use the xmlvlidatingreader class to verify XML documents and XML fragments. Xmlvalidatingreader class works on XML reader --- a typical xmltextreader class instance. Xmltextreade is used to read the document node, but xmlvlidatingreader verifies Each XML Block Based on the required verification type.

The xmlvlidatingreader class only provides a subset of essential functions for small XML readers. This class always works on an existing XML reader, which monitors methods and attributes. If you go deep into the constructor of this class, you will find that it relies heavily on an existing text reader. An XML reader with verification cannot be serialized directly from a file or a URL. The constructor list of this class is as follows:

Public xmlvalidatingreader (xmlreader );

Public xmlvalidatingreader (stream, xmlnodetype, xmlparsercontext );

Public xmlvalidatingreader (string, xmlnodetype, xmlparsercontext );

The XML reader with verification can analyze any XML fragments, which are provided by a string or stream or the XML documents provided by any reader.

Xmlvlidatingreader has very few methods with significant changes (compared with other reader classes). In addition, for read, it has Skip and readtypedvalue methods. The Skip method skips all the child nodes of the current node (you cannot skip XML text in bad format, it is quite usefulAlgorithm), The Skip method also verifies the skipped content. The readtypedvalue method returns the CLR type corresponding to the specified XML Schema (XSD) type. If the method finds the CLR type corresponding to the XSD type, the type name of CLR is returned. If no value is found, the node value is returned as a string value.

The XML reader with verification is just like its name. It is a node-based reader that verifies whether the structure of the current node conforms to the current schema. The verification is incremental; there is no way to return a Boolean value indicating whether the document is valid. Generally, you use the read method to read the input XML document. In fact, you can also use a reader with verification to read XML documents. In each step, whether the structure of the currently accessed node matches the specified schema or not. If not, an exception is thrown. Figure 4 is a console applicationProgramIt has a command line for entering the file name, and finally outputs the verification result.

Figure 4 console app

Using system;

Using system. xml;

Using system. xml. Schema;

 

Class myxmlvalidapp

{

Public myxmlvalidapp (string filename)

{

Try {

Validate (filename );

}

Catch (exception e ){

Console. writeline ("error:/t {0}", E. Message );

Console. writeline ("exception raised: {0 }",

E. GetType (). tostring ());

}

}

 

Private void validate (string filename)

{

Xmltextreader XTR = new xmltextreader (filename );

Xmlvalidatingreader vreader = new xmlvalidatingreader (XTR );

Vreader. validationtype = validationtype. Auto;

Vreader. validationeventhandler + = new

Validationeventhandler (this. validationeventhandle );

 

Vreader. Read ();

Vreader. movetocontent ();

 

While (vreader. Read ()){}

 

XTR. Close ();

Vreader. Close ();

}

 

Public void validationeventhandle (Object sender,

Validationeventargs ARGs)

{

Console. Write ("validation error:" + args. Message + "/R/N ");

}

 

Public static void main (string [] ARGs)

{

Myxmlvalidapp o = new myxmlvalidapp (ARGs [0]);

Return;

}

}

 

The validationtype attribute sets the verification type, which can be DTD, XSD, XDR, or none. If no verification type is specified (the validationtype. Auto option is used), the reader automatically uses the most suitable verification type based on the document. If any error occurs during verification, the validationeventhandler event is triggered. If the event validationeventhandler event handler is not provided, an XML exception is thrown. Defining the validationeventhandler event handler is a method used to capture XML exceptions caused by any errors in the XML source file. It should be noted that the reader's principle is to check whether a document is in good format and whether the document is in line with the architecture. If a verified reader finds an XML document with a serious format error, only the xmlexception exception is triggered and no other events are triggered.

Verification occurs when the user uses the read method to move the Pointer Forward. Once the node is analyzed and read, it obtains the internal objects passed for processing verification. The verification operation is based on the node type and the required verification type. It determines whether all the attributes of the node and the Child Nodes contained in the node meet the verification conditions.

The verification object internally calls two different styles of objects: DTD analyzer and schema builder ). The DTD analyzer processes the content of the current node and the Child tree that does not conform to the DTD. The architecture builder constructs a som (schema object model) for the current node based on the XDR or XSD architecture ). The schema generator class is actually the base class specified as the XDR and XSD Schema generators. Why? Although many of the same methods in the XDR and XSD architectures have been processed, their performance does not differ during execution.

If the node has a subnode, use another temporary reader to collect the subnode information. Therefore, the node architecture information can be fully verified. You can see Figure 5:

Note: although the xmlvalidatingreader class constructor can accept an xmlreader class as its reader, the reader can only be an instance of the xmltextreader class or an instance of its derived class. This means that you cannot use other classes derived from xmlreader (for example, a custom XML reader ). Inside the xmlvalidatingreader class, it assumes that the reader is a sub-xmltextreader object and explicitly converts the incoming reader to the xmltextreader class. If you use xmlnodereader or a custom reader, an error occurs during compilation and an exception is thrown during running.

Node Reader

XML reader provides an incremental method (read by one node) to process the document content. So far, we assume that the source file is a hard disk-based stream or a string stream. However, we cannot guarantee that an xmldom object of the source file will be provided to us. In this case, we need a special class with a special read method. In this case, the. NET Framework provides the xmlnodereader class.

Like xmltextreader accessing all nodes in a specified XML Stream, the xmlnodereader class accesses all nodes in the xmldom subtree. The xmldom class (xmldocument class in. NET Framework) supports XPath-based methods, such as the selectnodes method and selectsinglenode method. These methods are used to place matching nodes in the memory. If you need to process all nodes in the subtree, the node reader is more efficient than the node Reader using the incremental method:

// Xmldomnode is the xml dom Node

Xmlnodereader nodereader = new xmlnodereader (xmldomnode );

While (nodereader. Read ())

{

// Do something here

}

When you want. when referencing custom data in the cofig file, the data is first filled in the xmldom tree, and then processed in combination with the xmlnodereader class and the xmldom class. This is also efficient.

 

 

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.