C # XML programming-XmlValidatingReader class for XML document verification (7)

Source: Internet
Author: User

 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

  

 

  

 

 

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.