Easy processing of XML data in the. NET Framework (3-1)

Source: Internet
Author: User
?? Browser with authentication

?? The XmlValidatingReader class implements the XmlReader class, which provides support for multiple types of XML validation: Dtd,xml-data Reduced (XDR) architecture, and XSD,DTD and XSD are all officially recommended. XDR is a pattern that Microsoft used to process XML schemas earlier.

?? You can use the Xmlvlidatingreader class to validate XML documents and XML fragments. The XmlValidatingReader class works on top of an XML browser---is an exemplary example of a XmlTextReader class. Xmltextreade is used to read the document's nodes, but Xmlvlidatingreader validates each XML block based on the type of authentication required.

?? The Xmlvlidatingreader class implements only a subset of the functions necessary for a very small XML browser. The class always works on an existing XML browser, and it oversees methods and properties. If you go deep into the structure of the class, you will invent it that is obviously attached to an existing text browser. An XML browser with validation cannot be serialized directly from a file or a URL. The list of structural functions for this class is as follows:

Public XmlValidatingReader (XmlReader);

Public XmlValidatingReader (Stream, XmlNodeType, XmlParserContext);

Public XmlValidatingReader (String, XmlNodeType, XmlParserContext);

?? An XML browser with validation can parse any XML fragment, the XML fragment is supplied through a string or a stream, or an XML document supplied by any browser can be parsed.

?? There are very few methods for significant transformations 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 bad pattern of XML text, it is a fairly useful algorithm), and the Skip method also validates the skipped content. The ReadTypedValue method returns the CLR type that corresponds 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.

?? The XML browser with validation, as its name, is a node-based browser that verifies that the current node's structure conforms to the current schema. The validation is incremental; It has no method to return a Boolean value that shows whether the document is valid. Usually you read the XML document that you have entered into by using the Read method. In fact, you can also read an XML document with a validated browser. At each step, the structure of the currently visited node conforms to the specified schema and throws an exception if it is not met. Figure Four is a control console using the program, it has a command line to enter the file name, and finally output 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 above is the easy processing of XML data (3-1) in the. NET framework, and more about topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.