Use XML schema to validate XML documents : Schema « XML « C# / C Sharp






Use XML schema to validate XML documents

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

public class Validate {
  public static void Main(string [] args) {

    using (Stream stream = File.OpenRead(args[0])) {
      XmlValidatingReader reader = new XmlValidatingReader(new XmlTextReader(stream));
      reader.ValidationType = ValidationType.Schema;
      reader.Schemas.Add("", args[1]);
      reader.ValidationEventHandler += new ValidationEventHandler(Handler);

      while (reader.Read()) {
      }
    }
    Console.WriteLine("Document is valid.");
  }

  public static void Handler(object sender, ValidationEventArgs e) {
    Console.WriteLine(e.Message);
  }
}

           
         
    
    
  








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 XmlReaderSettings to validate the Xml document
6.Strip Non Valid XML Characters.
7.Is Well Formed Xml
8.XmlSchema is an in-memory representation of an XML Schema
9.Is Xml Valid
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