Is Xml Valid : Schema « XML « C# / C Sharp






Is Xml Valid

  

using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Reflection;

namespace Winsmarts.PI.Common
{
    public static class Utilities
    {
        public static bool IsXmlValid(string xmlFile, string xmlSchema)
        {
            TextReader schemaStream =
                new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(xmlSchema));
            XmlSchemaSet schemaSet = new XmlSchemaSet() ;
            schemaSet.Add("",XmlReader.Create(schemaStream));

            Stream docStream = new FileStream(xmlFile, FileMode.Open, FileAccess.Read);
            XmlReaderSettings settings = new XmlReaderSettings() ;
            settings.Schemas.Add(schemaSet) ;
            settings.ValidationType = ValidationType.Schema;
            XmlReader xRead = XmlReader.Create(docStream, settings)  ;

            bool toReturn = false;

            try
            {
                while (xRead.Read())
                {
                    // do nothing :-/
                }
                toReturn = true;
            }
            catch (XmlSchemaValidationException xExcp)
            {
                Trace.WriteLine("Input document " + xmlFile + " does not match the schema", true);
                Trace.WriteLine(xExcp.ToString(), true);
            }

            return toReturn;
        }
    }
}

   
    
  








Related examples in the same category

1.Set XmlReaderSettings
2.Choose ValidationType
3.Validate an XML Document Against a Schema
4.Validate Schema
5.Use XML schema to validate XML documents
6.Use XmlReaderSettings to validate the Xml document
7.Strip Non Valid XML Characters.
8.Is Well Formed Xml
9.XmlSchema is an in-memory representation of an XML Schema
10.Xml Validation Helper
11.Get Intrinsic Simple Types Names from System.Xml.Schema.DatatypeImplementation
12.Reads a XML schema file and returns the information found in that.
13.XML reading functionality