The. NET framework handles XML data easily (iii)

Source: Internet
Author: User
Tags command line constructor xml reader

Reader with validation

The XmlValidatingReader class implements the XmlReader class, which provides support for multiple types of XML validation: the dtd,xml-datareduced (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);

An XML reader with validation 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.

The XML reader with validation, 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
}
}

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.