By Robert Strauch

2013-06-21 10:05:53 8 Comments

I have a schema file which does not define any target namespaces, i.e. its definition looks like this:

<xs:schema xmlns:xs="" elementFormDefault="qualified">
  <!--Elements, attributes, etc. -->

The corresponding XML looks like this:

<Documents p1:CRC="0" p1:Date="1900-01-01T01:01:01+01:00" p1:Name="Test" p1:Status="new" xmlns:p1="" xmlns="">
  <p1:Document p1:Date="2010-12-23T07:59:45" p1:ErrorCode="0" p1:ErrorMessage="" p1:Number="TEST00001" p1:Status="new"/>

Validation of this XML against the schema with e.g. Altova XMLSpy or Oxygen XML Editor fails.

However my validation in C# (.NET 4.0) does not fail. The XML is processed as an XDocument object. If I have understood correctly then XDocument.Validate() does a lax validation if no namespace is found in the schema. Thus the validation does not fail. But then how can I implement a "strict" validation for XDocument?

This is how I try to validate the XML:

public static void ValidateXml(XDocument xml, string xsdFilename) {
  XmlReaderSettings settings = new XmlReaderSettings();
  XmlSchemaSet schemaSet = new XmlSchemaSet();

  schemaSet.Add(string.empty, xsdFilename);
  settings.ValidationType = ValidationType.Schema;
  settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
  settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);

  xml.Validate(schemaSet, ValidationCallback);

private static void ValidationCallback(object sender, ValidationEventArgs args) {
  if (args.Severity == XmlSeverityType.Warning) {
    // Do warning stuff...
  } else if (args.Severity == XmlSeverityType.Error) {
    // Do error stuff...


@Martin Honnen 2013-06-21 10:39:19

I am not sure it is possible to use the Validate method; if you use a validating XmlReader over the XDocument where ValidationFlags are set up to emit validation warnings, as in

        XDocument doc = XDocument.Load("../../XMLFile1.xml");

        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.Add(null, "../../XMLSchema1.xsd");

        XmlReaderSettings xrs = new XmlReaderSettings();
        xrs.ValidationType = ValidationType.Schema;
        xrs.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
        xrs.Schemas = schemaSet;
        xrs.ValidationEventHandler += (o, s) => {
            Console.WriteLine("{0}: {1}", s.Severity, s.Message);

        using (XmlReader xr = XmlReader.Create(doc.CreateReader(), xrs))
            while (xr.Read()) { }

then the ValidationEventHandler does emit a warning for each node it does not find schema information for. So your ValidationEventHandler could check for such warnings. But you might as well simply compare the doc.Root.Name.Namespace with the target namespace of the schemas you have before calling the Validate method.

Related Questions

Sponsored Content

38 Answered Questions

[SOLVED] Post parameter is always null

8 Answered Questions

[SOLVED] How can I ensure that a division of integers is always rounded up?

  • 2009-05-28 14:37:42
  • Karsten
  • 58438 View
  • 238 Score
  • 8 Answer
  • Tags:   c# math

6 Answered Questions

2 Answered Questions

[SOLVED] XSD validation with XmlSchemaSet does not find error

1 Answered Questions

2 Answered Questions

[SOLVED] How to create a checkbox list?

2 Answered Questions

[SOLVED] "The specified type was not recognized" when desearalizing a XML file using auto-generated schema

  • 2013-09-24 20:32:58
  • Scott Chamberlain
  • 2752 View
  • 2 Score
  • 2 Answer
  • Tags:   c# xml xsd

1 Answered Questions

[SOLVED] How do I fix "Could not find schema..." error while validating XML against XSD?

  • 2011-06-03 13:04:43
  • Paul Mrozowski
  • 4146 View
  • 2 Score
  • 1 Answer
  • Tags:   c# xml xsd

1 Answered Questions

xsd schema not valid?

  • 2010-01-23 17:57:55
  • Yaron Naveh
  • 689 View
  • 0 Score
  • 1 Answer
  • Tags:   c# .net xsd

Sponsored Content