Use XMLValidatingReader class to verify read XML documentation
The .NET assembly system.xml contains many classes that are used to provide XML features on the .NET platform. The XMLValidatingReader class (an implementation of the XMLReader class) is one of them. This class provides verification support when reading an XML document or XML fragment into the system. It implements the validity constraints defined by the DTD, XML Data Simplification (XDR) architecture and XML architecture definition language (XSD) architecture.
1. Construct an XMLVALIDATIADE object instance
Initializing new instances of the XMLValidatingReader class have multiple methods, the most commonly used parameters are incoming XMLReader types:
Public XMlValidatingReader (XMLReader Reader); XMLValidatingReader Vreader = New XmlValidatingReader (XMLTextReader XTR);
As one of the specific implementations of XMLReader, the XMLTextReader class provides quick, only forward, unpaid reads of XML documents, while XmlValidatingReader can use all content returned from XMLTextReader and further provide verification support. Of course, if everything is normal, the process does not cause loss of information, all nodes and properties returned from a given XMLReader return from this verification reader. The new node that is not returned from the base reader may be added to this reader (for example, the default attributes and child levels) of the entity reference.
2, specify the verification type
From the previous item you already know, there are three specifications for performing verification of XML documents. They are DTD, XDR and Xmlschema. Therefore, before performing verification, you need to determine the type of verification, which is done by setting the value of the ValidatingReader class:
VReader.validationType = validationtype.schema.
This line of code will verify that the declaration is XSD Schema.
3, use the XMLSChemacollection class cache schema
If you need to verify it according to XDR or XSD Schema, you can use the XMLSChemacollection class to cache schema so that performance will be improved. The Add method of the XMLSChemacollection class loads the architecture, and the architecture is associated with the namespace URI. This is usually a target of the schema for the "XML Architecture" source file (.xsd).
Xmlschemacollection XSC = New Xmlschemacollection (); xsc.add ("http://www.tuha.net", "vschema.xsd");
Of course, if the architecture is in an XML document, it doesn't matter.
4, associated architecture cache
After adding Schema to Xmlschemacolle, XMLValidatingReader does not automatically identify and use Schema, and needs to be associated with both. Complete this process by using the Schemas attribute of the reader to complete the architecture file in XMLsChemacollection to complete this process:
Vreader.schemas.Add (XSC);
5, ValidationEventHandler event handler callback
Accidents may occur when using ValidatingReader execution verifying reading an XML document. At this time, verification errors and warnings can be validated by ValidationEventHandler callback. The ValidationEventHandler event is used to set an event handler to receive information about document type definition (DTD), data simplified XML (XDR) and XML Architecture Definition Language (XSD) schema verification error. However, if you do not provide ValidationEventHandler, you can still use a common exception handling mechanism to capture errors. When an analytical error occurs, it will be reported by trigger XMLException. If a verification error occurs, XmlschemaException will be thrown. Of course, any exception will not be able to restart XMLValidatingReader.
Specify events and callbacks Follow general practices: Connect XMLValidatingReader and event handler ValidationEventHandler via =:
VReader.validationEventHandler = New ValidationEventHandler (vcallback);
The parameter vCallback is the method name of the callback handler. This method must contain a parameter of a ValidationEventArgs type. The ValidationEventArgs class has properties for the following: text messages, indicating ERROR or WARNING XMLSeverType enumeration, and contains compliance with specific validation errors The exception of XMLSChemaException information.
: Private Void Vcallback (Object sender, validationEventArgs args) {// Processing code when an error occurs}
This step is not necessary. If you can guarantee that the error will not happen or happen!
6, execute verify read operations
Once you have finished preparing above, you can use the XMLValidatingReader class read method to start verifying the read XML document. Once any of READ, READINNERXML, READOUTERXML, and other methods that will change the contact, such as a Skip () method. At this time, verification will occur.
While (VReader.Read ()) {// Processing the content of the read}
Third, instance
Comprehensive knowledge, create a Windows Console console application, used to handle product data in the business domain, in general, different company product data will follow certain formats, here via XSD
XSD, this architecture file provides structural information for XML documents, used to follow the consistent criteria during data exchange
XML Version = "1.0" encoding = "UTF-8"?>
XML Version = "1.0" encoding = "utf-8"?>
The following application processes the XML document and verifies that its data is in line with this architecture!
using System; using System.IO; using System.Xml; using System.Xml.Schema; namespace MyXmlValidationgReader {class Class1 {static bool sign = true; [STAThread] 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); // Configure the non-verified reader XMLSChemacollection xsc = new xmlschemacollection (); // Constructs the Schema architecture Cache Xsc.Add ("http://www.tuha.net", xsdfile); // Add Architecture file and corresponding name space xvr = new xmlvalidatingReader (xtr); // Configuring the reader xvr.schemas.add (xsc); // Association verification reader and architecture set xvr.validationType = validationType.Schema; // Set the verification type to schema architecture xvr.validationEventhandler = new value, the event handler when the error occurs, when an error occurs, the read operation {} console.write ("Finished!" SIGN .Tostring ());} private static void vcallback (Object sender, validationEventargs args) // Error Tune {sign = false; // Change Tag}}} Reference: "XML Advanced Programming", "XSLT Advanced Programming", MSDN