Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I validate XML against XSD (separate documents) in DNX Core 5.0 (ASP.NET 5)?

I am porting some code to ASP.NET 5, and want to target DNX Core 5.0. However, I am having trouble locating the types that are required to validate an XML document against an XSD document.

Here is the code:

var xsdStream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(xsdPath);
using (XmlReader xsd = XmlReader.Create(xsdStream))
{
    XmlSchemaSet schema = new XmlSchemaSet();
    schema.Add(null, xsd);

    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();

    xmlReaderSettings.ValidationType = ValidationType.Schema;
    xmlReaderSettings.Schemas.Add(schema);

    using (XmlReader xmlReader = XmlReader.Create(xmlPath, xmlReaderSettings))
    {
        try
        {
            while (xmlReader.Read());
        }
        catch (Exception ex)
        {
            throw new Exception(string.Format(Resources.Messages.XmlValidationFailed, xmlPath), ex);
        }
    }
}

As you can see, all I want is to stop on the first error and throw an exception indicating what the error is.

The problems are:

  1. The XmlSchemaSet class doesn't exist in the System.Xml.Schema namespace (or anywhere else I have found).
  2. The XmlReaderSettings.ValidationType and XmlReaderSettings.Schemas properties do not exist.

I checked the MSDN Documentation which has a slightly different approach. However, as before XmlSchemaSet doesn't exist, and neither does XDocument.Validate(). I have also searched several of the ASP.NET projects for an example but can't seem to find any.

What facilities (if any) exist in DNX Core 5.0 to validate XML against an XSD? I would prefer to do this using streams if possible, but if absolutely necessary I will accept an approach that reads the entire documents into memory at once.

like image 394
NightOwl888 Avatar asked Sep 30 '15 17:09

NightOwl888


1 Answers

There is no support for XSD in the first release. When I heard right in one of the tweets, posts, bugs or community standups they do, it is considered for a later release.

ps: Pawel should answer this and get the credits ... but we should close this question.

like image 88
Thomas Avatar answered Oct 13 '22 22:10

Thomas