Easily handle XML data in .NET Framework (3)

xiaoxiao2021-03-06  40

With the verified reader XMLValidatingReader class implements the XMLReader class, which provides multiple 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. It follows a list of the class constructor: public XmlValidatingReader (XmlReader); public XmlValidatingReader (Stream, XmlNodeType, XmlParserContext); public XmlValidatingReader (string, XmlNodeType, XmlParserContext); XML reader can verify with any XML fragment analysis, XML fragment by A String or a Stream is provided or an XML document provided by any reader. 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);" 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) {co NSOLE.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.

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

New Post(0)