C # several methods for verifying XML in Schema

Source: Internet
Author: User
Schema is a language used to describe and standardize the logical structure of XML documents. It is used to verify the correctness of the logical structure of XML files. It can be understood as similar to DTD (document type definition), but schema is superior in the current web development environment. Because it is a valid XML document, you can understand the XML structure more intuitively. In addition, the schema supports namespaces, multiple simple and complex built-in data types, and custom data types. With so many advantages, schema has gradually become a unified specification for XML applications.

 

 

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. xml. Schema;
Using system. xml;
Using system. IO;
Namespace myclass1
{
Public class testschema
{
Private Static string namespace = "http://www.beyondbit.com ";
Private Static bool isvalid = true;
Public static bool validationschema (string filename)
{
Xmltextreader r = new xmltextreader (filename );
Xmlvalidatingreader v = new xmlvalidatingreader (R );
V. validationtype = validationtype. Schema;
V. validationeventhandler + = new validationeventhandler (myvalidationeventhandler );
While (V. Read ())
{
// Can add code here to process the content.
}
V. Close ();
Return isvalid;
}
Public static bool validationschemanew (string filename, string schema)
{
Xmlschemaset xsset = new xmlschemaset ();
Try
{
Xsset. Add (namespace, schema );
}
Catch (exception ex ){
String A = ex. message;
Return false;
}
// Define how to use the document mode
Xmlreadersettings xrsetting = new xmlreadersettings ();
Xrsetting. validationtype = validationtype. Schema;
// Associate the verification reader with the schema set
Xrsetting. schemas = xsset;
// Add the event processing when an error occurs Program
Xrsetting. validationeventhandler + = new validationeventhandler (myvalidationeventhandler );
// Use the latest method to build a reader that can be verified and construct a verification Reader
Xmlreader xr = xmlreader. Create (filename, xrsetting );
// All document nodes of cyclic Detection
While (XR. Read ())
{
}
XR. Close ();
Return isvalid;
}
Public static bool validationschemanow (string xmlstring, string schemastring)
{
// Construct the xmlreader object of the data to be verified
Xmlreader xmldatareader = xmltextreader. Create (New stringreader (xmlstring ));
// Construct a standard xmlreader object
Xmlreader xmlschemareader = xmltextreader. Create (New stringreader (schemastring ));
// Define how to use the document mode
Xmlreadersettings xrsetting = new xmlreadersettings ();
Try
{
Xrsetting. schemas. Add (null, xmlschemareader );
}
Catch (system. xml. schema. xmlschemaexception ex)
{
String B = ex. message;
Return false;
}
Xrsetting. validationtype = validationtype. Schema;
Xrsetting. validationeventhandler + = new validationeventhandler (myvalidationeventhandler );
Xmlreader xr = xmlreader. Create (xmldatareader, xrsetting );
While (XR. Read ())
{
}
XR. Close ();
Return isvalid;
}
/// <Summary>
/// Verification process
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "ARGs"> </param>
Private Static void myvalidationeventhandler (Object sender, validationeventargs ARGs)
{
If (ARGs. Severity = xmlseveritytype. Warning)
{
Writeerrorlogs ("Validation warning:" + args. Message );
}
Else
{
Writeerrorlogs ("validation error:" + args. Message );
}
Isvalid = false;
}
/// <Summary>
/// Record error logs
/// </Summary>
/// <Param name = "SS"> </param>
Private Static void writeerrorlogs (string SS)
{
Streamwriter Sw = NULL;
Try
{
Sw = new streamwriter (@ "C: \ test_error.txt", true, encoding. utf8 );
Sw. writeline ("[" + datetime. Now. tostring () + "]" + SS );
Sw. Flush ();
Sw. Close ();
}
Catch {Sw. Close ();}
}
}
}

Read XML documents using xmlvalidatingreader class
The. NET assembly system. xml contains many classes that provide XML functionality on the. NET platform. Xmlvalidatingreader class (an implementation of the xmlreader class) is one of them. This class provides verification support when you read XML documents or XML fragments into the system. It implements the validity constraints defined by the DTD, XML data simplified (XDR) architecture, XML Architecture Definition Language (XSD) architecture, and other specifications.

1. Construct an xmlvalidatingreade class object instance
There are multiple methods to initialize the new instance of the xmlvalidatingreader class. The most common method is to pass in the xmlreader type parameter: Public xmlvalidatingreader (xmlreader reader );
Xmlvalidatingreader vreader = new xmlvalidatingreader (xmltextreader XTR );

As one of the specific implementations of xmlreader, The xmltextreader class provides fast, forward-only, and cache-free reading of XML documents, while xmlvalidatingreader can use all the content returned from xmltextreader, and further provide verification support. Of course, if everything works normally, this process will not cause information loss, and all nodes and attributes returned from the given xmlreader will also be returned from the verification reader. New nodes not returned from the base reader may be added to this reader (for example, default attributes and sublevels of object references ).

2. Specify the verification type
As you can see earlier, there are three specifications used to verify the XML document. They are DTD, XDR, and XMLSCHEMA. Therefore, before performing the verification, You need to determine the verification type, which is completed by setting the validationtype attribute of the validatingreader class:

Vreader. validationtype = validationtype. schema.

This lineCodeDeclare the verification as XSD Schema.

3. Use xmlschemacollection class Cache architecture

If you need to verify against XDR or XSD Schema, you can use the xmlschemacollection class to cache the architecture, which will improve the performance. The add method of the xmlschemacollection class loads the schema, And the schema is associated with the namespace URI. For the source file "XML schema" (. XSD), this is usually the targetnamespace attribute of the schema.

Xmlschemacollection xsc = new xmlschemacollection ();
Xsc. Add ("http://www.tuha.net", "vschema. XSD ");

Of course, this is not required if the architecture is inline in the XML document.

4. Associated architecture Cache

After adding a schema to xmlschemacollection, xmlvalidatingreader cannot automatically identify and use the schema. This process is completed by referencing the schema file cached in xmlschemacollection using the reader's schemas attribute:

Vreader. schemas. Add (xsc );

5. validationeventhandler event handler callback
An error may occur when validatingreader is used to read the XML document for verification. In this case, you can report verification errors and warnings through the validationeventhandler callback. Validationeventhandler events are used to set an event handler to receive information about Document Type Definitions (DTD), simplified XML (XDR), and XML Schema Definition Language (XSD) Schema validation errors.

However, if validationeventhandler is not provided, you can still use a general exception processor to capture errors. When an analysis error occurs, the xmlexception is reported. If a verification error occurs, xmlschemaexception is thrown. Of course, xmlvalidatingreader cannot be restarted due to any exception.

The specified event and callback follow the common practice: connect xmlvalidatingreader to the event handler validationeventhandler through + =:

Vreader. validationeventhandler + = new validationeventhandler (vcallback );

The parameter vcallback is the method name of the callback handler. This method must contain a parameter of the validationeventargs type. The validationeventargs class has attributes for the following items: text message, indicates the xmlseveritytype enumeration of error or warning, and contains the xmlschemaexception information associated with a specific verification error.

:
Private void vcallback (Object sender, validationeventargs ARGs)
{
// Processing code when an error occurs
}

This step is not necessary. If you can ensure that errors do not occur or happen, proceed!

6. Perform read Verification

After completing the above preparations, you can use the read method of the xmlvalidatingreader class to verify the reading of the XML document. It can be any of the read, readinnerxml, readouterxml methods, and other methods that will change the contact, such as the SKIP () method. At this time, verification will occur.

While (vreader. Read ())
{
// Process read content
}

Iii. Instances
Based on the above knowledge, we will create a Windows console application to process product data in the business field. Generally, the product data of different companies will follow a certain format.

XSD. This schema file provides structure information for XML documents, which follows consistent standards during data exchange. <? XML version = "1.0" encoding = "UTF-8"?>
<Xs: schema id = "Products" targetnamespace = "http://www.tuha.net" elementformdefault = "qualified"
Xmlns = "http://www.tuha.net" xmlns: mstns = "http://www.tuha.net"
Xmlns: xs = "http://www.w3.org/2001/XMLSchema">
<Xs: element name = "Products" type = "A1"> </Xs: Element>
<Xs: complextype name = "B1">
<Xs: sequence>
<Xs: element name = "name" type = "XS: string"/>
<Xs: element name = "type" type = "XS: string"/>
<Xs: element name = "usefor" type = "XS: string"/>
</Xs: sequence>
</Xs: complextype>
<Xs: complextype name = "A1">
<Xs: sequence maxoccurs = "unbounded">
<Xs: element name = "item" type = "B1"/>
</Xs: sequence>
</Xs: complextype>
</Xs: schema> according to the preceding architecture file, an XML document of product data is constructed below. Some sections of the product library content are used here to facilitate testing, make it a complete XML document: <? XML version = "1.0" encoding = "UTF-8"?>
<Roducts xmlns = "http://www.tuha.net">
<Item>
<Name> talking online </Name>
<Type> fitbench </type>
<Usefor> communicate </usefor>
</Item>
<Item>
<Name> debugging online </Name>
<Type> rofessional </type>
<Usefor> machine </usefor>
</Item>
</Products> the following applications process the XML document and verify that the data conforms to the schema! Using system;
Using system. IO;
Using system. xml;
Using system. xml. Schema;
Namespace myxmlvalidationgreader
{
Class class1
{
Static bool Sign = true;
[Stathread]
Static void main (string [] ARGs)
{
Xmltextreader XTR = NULL;
Xmlvalidatingreader xvr = NULL;
String xmlfile = "http://www.cnblogs.com/Products.xml"; // XML Source Document
String xsdfile = "http://www.cnblogs.com/Products.xsd"; // XSD Schema document
XTR = new xmltextreader (xmlfile); // construct a non-verified Reader
Xmlschemacollection xsc = new xmlschemacollection (); // construct schema Cache
Xsc. Add ("http://www.tuha.net", xsdfile); // Add the schema file and corresponding namespace in the cache
Xvr = new xmlvalidatingreader (XTR); // construct a verification Reader
Xvr. schemas. Add (xsc); // associate the reader with the schema set
Xvr. validationtype = validationtype. Schema; // you can specify schema as the verification type.
Xvr. validationeventhandler + = new validationeventhandler (vcallback );
// Event handler when an error occurs
While (xvr. Read () // executes the read operation
{
}
Console. Write ("finished! "+ Sign. tostring ());
}
Private Static void vcallback (Object sender, validationeventargs ARGs)
// Error callback Program
{
Sign = false; // change the flag
}
}
}

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.