Reading directory
I. Preface
II. Implementation steps
Iii. Running Effect
Preface
XML document verification indicates whether the XML document complies with the rules and is a correct XML document. Here we will use the XmlValidatingReader class to verify the XML document, this class implements the validity constraint DTD defined by W3C, simplifies the XDR data, and verifies the XML Schema Definition Language XSD. If the verification result proves that the XML document is valid, otherwise it is invalid.
The XmlValidatingReader class implements the XmlReader class and verifies the XML Stream in a forward-only manner.
First, let's look at the XML architecture diagram below. The figure shows that the child element under the <BookShop> element must be <Book>. The originally closed tag should be <Book>, but we have written it as <Boo>. Let's see if this incorrect XML document can pass verification.
II. Implementation steps
1: Write an XML file
1.1 BookShop. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<BookShop xmlns = "http://tempuri.org/BookShop.xsd">
<Book> Journey to the West </Book>
<Boo> Water Margin </Boo>
</BookShop>
2: code file writing
2.1 Form1.cs
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. IO;
Using System. Xml. Schema;
Using System. Xml;
Namespace XmlReaderSettingsClass
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void Form1_Load (object sender, EventArgs e)
{
String strXmlPath = "http://www.cnblogs.com/BookShop.xml ";
String strXmlXSDPath = "http://www.cnblogs.com/BookShop.xsd ";
// Open the BookShop. xml file into a stream
FileStream stream = new FileStream (strXmlPath, FileMode. Open );
// Create an object of the XmlValidatingReader class
XmlValidatingReader xvr = new XmlValidatingReader (stream, XmlNodeType. Element, null );
// Load the XML architecture document
Xvr. Schemas. Add (null, strXmlXSDPath );
// The verification method is based on the XML architecture.
Xvr. ValidationType = ValidationType. Schema;
// Pre-define an event
Xvr. ValidationEventHandler + = new ValidationEventHandler (ValidationHandler );
// Verify the document
While (xvr. Read ());
// Display that the verification process is complete
MessageBox. Show ("verification completed ");
// Close the opened file
Stream. Close ();
}
Private void ValidationHandler (object sender, ValidationEventArgs args)
{
// Display the verification failure message
MessageBox. Show ("Verification Failed:" + args. Message );
}
}
}
Iii. Running Effect