XML Schema Validator Class (from uabv2.0)

xiaoxiao2021-03-05  35

There is an XML architecture verification class, which is taken from Microsoft Updater Application Block V2.0.

Source code:

/ / =========================================================================================================================================================================================== ============================================================================================================================================================================================================= ==========

// Microsoft Updater Application Block for .NET

// http://msdn.microsoft.com/library/en-us/dnbda/html/Updater.asp

//

// Schemavalidator.cs

//

// Contains the Implementation of The Schema Validator.

//

// for more information revelementation overview.

//

/ / =========================================================================================================================================================================================== ============================================================================================================================================================================================================= ==========

// Copyright? Microsoft Corporation. All Rights Reserved.

// this code and information is provided "as is" without warranty

// of any kind, Either expressed or implied, incruding but not

// limited to the implied warranties of merchantability and

// Fitness for a particular purpose.//======================================== ============================================================================================================================================================================================================= ==================

Using system;

Using system.collections;

Using system.io;

USING SYSTEM.XML;

USING SYSTEM.XML.SCHEMA;

Namespace Microsoft.ApplicationBlocks.Updater.utilities

{

///

/// Helper Class to Perform Schema Validations.

///

Public Sealed Class SchemaValidator

{

#Region Private MEMBERS

///

/// The Schemas That Will BE Used for Validation.

///

Private arraylist schemas = new arraylist (2);

///

/// The Errors Detected During The Validation.

///

Private arraylist errors = new arraylist (5);

///

/// WHETHER THE Document Is Valid or Not.

///

Private bool isvalid;

#ndregion

#Region Constructionors

///

/// Creates An Instance of The Schemavalidator Using The Document and The Schemas.

///

/// The document to validate.

/// a list of schema to validate the document against.

Public SchemaValidator (String Document, Params Stream [] Schemas)

{

THIS.SCHEMAS.ADD (Document); Foreach (Stream S in schema)

{

This.Schemas.Add (s);

}

Isvalid = true;

}

#ndregion

#Region Public MEMBERS

///

/// Validates The Document and Returns The Result of The Validation.

///

/// true if the document has succeedededededededededededededededededed, Otherwise false .

Public bool validate ()

{

Errors.clear ();

XMLValidatingReader VR = NULL;

Object doc = schema [0];

IF (DOC Is Stream)

{

VR = New XMLValidatingReader (stream) DOC, XMLNODETYPE.ELEMENT, NULL;

}

Else IF (Doc is String)

{

VR = New XmlValidatingReader (String) DOC, XMLNodeType.ement, NULL

}

Try

{

For (int i = 1; i

{

vr.schemas.add (null, new xmltextreader (stream) Schemas [i]));

}

vr.validationEventhandler = new system.xml.schema.validationEventHandler (ValidationEventHandler);

While (Vr.Read ());

Return isvalid;

}

Finally

{

vr.close ();

}

}

///

/// Contains the validation error ENCOUNTERED in The Last Validation Operation.

///

Public validationEventArgs [] ErrorS

{

get

{

Return (ValidationEventArgs []) Errors.toArray (TypeOf (ValidationEventArgs);

}

}

///

/// Helper Method to Capture The Event Handlers.

///

/// The sender of the event.

/// The information about the event.

Private Void ValidationEventHndLer (Object Sender, System.xml.Schema.ValidationEventArgs E)

{

Errors.Add (e);

IF (e.severity == xmlseveritytype.warning)

{

Return;

}

isvalid = false;}

#ndregion

}

}

use:

///

/// Download and Validates The Manifest Using The Uri Location.

///

/// The Type That Reperesents the manifest class.

/// The location of the manifest.

/// The resource where the embedded xsd resides in the assembly.

/// The manifest instance.

[SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]

Private Object ValidateAndDeserialIze (Type Type, Uri location)

{

Object result = null;

Try

{

String Doc = Downloadfile (location);

USING (stream schema = askassembly.getexecutingassembly (). getManifestResourceStream (SchemareSource))

{

Schemavalidator Validator = New SchemaValidator (DOC, Schema);

IF (! Validator.Validate ())

{

Logger.logandthrowException (resource.resource.Manager [Resource.MessageKey.manifestschemaerror, location, schemaresource]);

}

XMLSerializer Xser = New XMLSerializer (TYPE);

Result = xser.deSerialize (New XMLTextReader (DOC, XMLNodetype.Document, NULL);

}

}

Catch (Exception EX)

{

Logger.logandthrowException (ex);

}

Return Result;

}

转载请注明原文地址:https://www.9cbs.com/read-34148.html

New Post(0)