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:
XmlSchemaSet
class doesn't exist in the System.Xml.Schema
namespace (or anywhere else I have found).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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With