Xml| Data Tape Verification reader
The xmlvalidatingreader class implements the XmlReader class, which provides support for multiple types of XML validation: the Dtd,xml-data reduced (XDR) architecture, and both XSD,DTD and XSD are officially recommended by the consortium. XDR is a format that Microsoft used to work with XML schemas earlier.
You can use the Xmlvlidatingreader class to validate XML documents and XML fragments. The XmlValidatingReader class works on an 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 type of validation required.
The xmlvlidatingreader class implements only a subset of the features that are necessary for a very small XML reader. The class always works on an existing XML reader, which monitors methods and properties. If you delve into the constructor of the class, you will find that it obviously relies on an existing text reader. XML readers with validation cannot be serialized directly from a file or from a URL. The constructor list for the class is as follows:
Public XmlValidatingReader (XmlReader);
Public XmlValidatingReader (Stream, XmlNodeType, XmlParserContext);
Public XmlValidatingReader (String, XmlNodeType, XmlParserContext);
The validated XML reader can parse any XML fragment, the XML fragment is provided by a string or a stream, and the XML document provided by any reader can be parsed.
There are very few methods for significant changes in the xmlvlidatingreader class (as opposed to other reader classes), and for Read, it has the skip and ReadTypedValue methods. The Skip method skips all child nodes of the current node (you cannot skip the malformed XML text, which is a fairly useful algorithm), and the Skip method verifies the skipped content. The ReadTypedValue method returns the CLR type corresponding to the specified XML schema (XSD) type. If the method finds a CLR type corresponding to the XSD type, the type name of the CLR is returned. If it is not found, the value of the node is returned as a string value.
A validated XML reader, like its name, is a node-based reader that verifies that the current node's structure conforms to the current schema. validation is incremental; it does not have a method to return a Boolean value that indicates whether the document is valid. You usually read the input XML document using the Read method. In fact, you can also read XML documents with a validated reader. In each step, whether the structure of the currently accessed node conforms to the specified schema, and if not, throws an exception. Figure Four is a console application that has a command line to enter a filename, and the final output of the validation results.
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 property sets the type of validation, which can be: DTD, XSD, XDR, or none. If you do not specify the type of validation (with the ValidationType.Auto option), the reader will automatically use the most appropriate type of validation based on the document. Any errors that occur during validation trigger the ValidationEventHandler event. If no event ValidationEventHandler event handler is provided, an XML exception is thrown. Defining a ValidationEventHandler event handler is a way to catch any errors in an XML source file that throw an XML exception. Note that the principle of the reader is to check whether a document is well-formed and to check if the document matches the schema. If a reader with validation discovers a malformed XML document, it only triggers the xmlexception exception, which does not trigger other events.
The verification occurs when the user moves the pointer forward with the Read method, and once the node is parsed and read, it gets the internal object of the processing validation that is transmitted over. The validation operation is based on the node type and the type of authentication required. It confirms that all of the node's properties and nodes contain child nodes that meet the validation criteria.
The validation object internally invokes two different styles of objects: The DTD parser and the schema Builder (schema builder). The DTD parser handles the contents of the current node and the subtree that does not conform to the DTD. The Schema builder constructs a SOM (schema object model) for the current node based on the XDR or XSD schema. The Schema builder class is actually all base classes that are specified as the XDR and XSD schema generators. Why, although many of the same methods of XDR and XSD schemas have been processed, they perform without distinction at performance.
If the node has child nodes, the node information is collected with another temporary reader, so the schema information of the nodes can be fully validated. You can look at figure five:
Note that although the constructor of the XmlValidatingReader class can accept a XmlReader class as its reader, the reader can only be an instance of a XmlTextReader class or an instance of its derived class. This means that you cannot use other classes derived from XmlReader (such as a custom XML reader). Inside the XmlValidatingReader class, it assumes that the reader is a XmlTextReader object and explicitly converts the incoming reader into a XmlTextReader class. If you use a xmlnodereader or a custom reader, the program will fail at compile time and throw an exception at runtime.
£Node Reader
The xml Reader provides an incremental approach (read by one node) to process the contents of a document. So far, we have assumed that the source file is a disk-based stream or a string stream, however, we cannot guarantee that a Xmldom object in the actual source file will be provided to us. In this case, we need a special class with a special reading method. In this case, the. NET Framework provides a XmlNodeReader class.
As XmlTextReader accesses all nodes in the specified XML stream, the XmlNodeReader class accesses all nodes of the XMLDOM subtree. The Xmldom class (The XmlDocument class in the. NET framework) supports XPath based methods, such as selectnodes methods and selectSingleNode methods. The purpose of these methods is to put the matching nodes in memory. If you need to deal with all the nodes in the subtree, the node reader is more efficient than the incremental method of processing the nodes ' reading devices:
XMLDOMNode is the XML DOM node
XmlNodeReader Nodereader = new XmlNodeReader (XMLDOMNode);
while (Nodereader.read ())
{
Do something here
}
When you want to reference custom data in a configuration file (such as a web.cofig file), populate the xmldom tree with the XmlNodeReader class and the XMLDOM class in conjunction with the data. It's also efficient.
Author: Chyich (translated)/aspcool