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.
/// summary>
Public Sealed Class SchemaValidator
{
#Region Private MEMBERS
///
/// The Schemas That Will BE Used for Validation.
/// summary>
Private arraylist schemas = new arraylist (2);
///
/// The Errors Detected During The Validation.
/// summary>
Private arraylist errors = new arraylist (5);
///
/// WHETHER THE Document Is Valid or Not.
/// summary>
Private bool isvalid;
#ndregion
#Region Constructionors
///
/// Creates An Instance of The Schemavalidator Using The Document and The Schemas.
/// summary>
/// The document to validate. param>
/// a list of schema to validate the document against. param>
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.
/// summary>
///
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. /// summary> Public validationEventArgs [] ErrorS { get { Return (ValidationEventArgs []) Errors.toArray (TypeOf (ValidationEventArgs); } } /// /// Helper Method to Capture The Event Handlers. /// summary> /// The sender of the event. param> /// The information about the event. param> 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. /// summary> /// The Type That Reperesents the manifest class. param> /// The location of the manifest. param> /// The resource where the embedded xsd resides in the assembly. param> /// [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; }