Easily handle XML data in .NET Framework (3)

zhaozj2021-02-17  52

Verified reader

The XMLValidatingReader class implements the XMLReader class, which provides a variety of types of XML authentication: DTD, XML-Data Reduced (XDR) architecture, and XSD, DTD, and XSD are recommended by W3C. XDR is a format for Microsoft to process the XML architecture.

You can use the XMLVLidatingReader class to verify the XML document and XML pieces. The XMLValidatingReader class works on the XML reader - is a typical XMLTextReader class instance. XMLTextReade is used to read the node of the document, but XMLVLidatingReader verifies each XML block based on the required validation type.

The XMLVLidatingReader class only implements a very small XML reader necessary for a functional subset. This class is always working on an existing XML reader that monitors methods and properties. If you go deep into this class constructor, you will find it clearly on an existing text reader. A verified XML reader cannot serialize directly from one file or a URL. The list of constructor of this class is as follows:

Public XMLValidatingReader (XMLReader);

Public XMLValidatingReader (stream, xmlnodetype, xmlparsercontext);

Public XMLValidatingReader (String, XMLNodType, XMLParserContext);

Validated XML reader can analyze any XML pieces, XML pieces are provided through a String or a stream, or the XML documentation provided by any reader can also be analyzed.

There is very little way in the XMLVLidatingReader class (relative to other Reader classes), and other For Read, it has SKIP and READTYPEDVALUE methods. SKIP methods Skip all child nodes of the current node (you can't skip the XML text of bad format, it is quite useful algorithm), 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 CLR type name is returned. If you can't find it, the value of the node is returned as a string value.

The verified XML reader is just a name, it is a node-based reader that verifies whether the structure of the current node complies with the current SCHEMA. Verification is incremental; it does not have a method to return a Boolean value indicating whether the document is valid. Usually you are using the Read method to read the entered XML document. In fact, you can also read the XML document with a validated reader. In each step, whether the structure of the node currently accessed is in line with the specified schema, if not, throw an exception. Figure 4 is a console application, which has a command line to enter the file name, and finally output 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.validationEventhandLene);

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 property sets the type of verification, which can be: DTD, XSD, XDR, or None. If you do not specify the type of verification (with the validationType.Auto option), the reader will automatically use the most suitable authentication type according to the document. Any errors occur during the verification process will trigger the ValidationEventHandler event. If an event ValidationEventHandler event handler is provided, an XML exception is thrown. Defining the ValidationEventHandler event handler is a way to capture any errors in an XML source file to raise an XML exception. It should be noted that the principle of the reader is to check if a document is well format, and check whether the document is consistent with the architecture. If a verified reader discovers an XML document with a serious format, it will only trigger XMlexception exception, which does not trigger other events.

Verify that when the user moves the pointer with the READ method, once the node is analyzed and read, it obtains an internal object that is transmitted. The verification operation is based on the node type and the required verification type. It confirms that the node all attributes and the child node containing the child feature complies with the verification criteria.

Verify objects call two different styles internally: DTD analyzer and architecture generator (Schema Builder). The DTD analyzer processes the contents of the current node and the subtree that does not conform to DTD. The architecture generator builds an SOM (Schema Object Model) based on the XDR or XSD architecture. The architecture generator class is actually a base class specified as an XDR and an XSD architecture generator. Why, although many of the XDR and XSD architectures are processed, they have no difference in performance during execution.

If the node has a child node, collects sub-node information with another temporary reader, so the architecture information of the node can be fully verified. You can see Figure 5:

Note that although the constructor of the XMLValidatingReader class accepts 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 you can't use other classes derived from XmlReader (such as 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, the program will be erroneous when compiling, and an exception is thrown. The Node Reader XML reader provides an incremental method (read by a node) to process the contents of the document. So far, we assume that the source file is a hard disk-based stream or a string stream, however, we cannot guarantee that the XMLDOM object of a source file will be provided in practice. In this case, we need a special class with a special reading method. This situation, .NET Framework provides an XMLNodeReader class. Just like XMLTextReader access to all nodes in the specified XML stream, the XMLNodeReader class accesses all nodes of the XMLDom subtree. XMLDOM classes (XMLDocument classes in .NET Framework) support xPath-based methods, such as SelectNodes methods, and SelectsingLenode methods. The role of these methods is to put the matching nodes in memory. If you need to handle all nodes in the subtree, the node reader has higher efficiency than the reader for processing nodes with incremental methods: // xmldomNode is the XML Dom Node XMLNodeReader NodeReader = New XMLNodeReader (XmLDomnode); while NodeReader.Read ()) {// do something Here} When you want to quote custom data in a configuration file (such as web.cofig file), pop the data into the XMLDOM tree, then use the XMLNodeReader class with XMLDom Class combined with these data. This is also efficient.

转载请注明原文地址:https://www.9cbs.com/read-29321.html

New Post(0)