作为我的问题transforming large Xml files的后续内容,我现在需要验证架构。
我正在使用这种扩展方法,可以明显改进,因为它无法正常工作
public static XElement ValidateXsd(this XElement source, string xsdPath)
{
var errors = new XElement("Errors");
// Reference: http://msdn.microsoft.com/en-us/library/bb358456.aspx
var xsd = XDocument.Load(xsdPath);
var xml = XDocument.Load(source.CreateReader());
var schemas = new XmlSchemaSet();
schemas.Add("", xsd.CreateReader());
if (xml.Document != null)
{
xml.Document.Validate(schemas,
// Validation Event/Error Handling
(sender, e) =>
{
var message = e.Message
.Replace(
"element is invalid - The value '' is invalid according to its datatype 'requiredString' - The actual length is less than the MinLength value.",
"cannot be blank.")
.Replace(
"is invalid according to its datatype 'size' - The Pattern constraint failed.",
"must be numeric.")
.Replace(
"element is invalid",
"is invalid.");
errors.Add(new XElement("Error", message));
}
);
}
// If there were errors return them, otherwise return null
return errors.Elements().Count() > 0 ? errors : null;
}
答案 0 :(得分:3)
请改为尝试:
public static XElement ValidateXsd(this XElement source, string xsdPath)
{
var errors = new XElement("Errors");
// Reference: http://msdn.microsoft.com/en-us/library/bb358456.aspx
var schemas = new XmlSchemaSet();
using (var reader = XmlReader.Create(xsdPath))
{
schemas.Add("", reader);
}
{
source.Document.Validate(
schemas,
// Validation Event/Error Handling
(sender, e) =>
{
var message =
e.Message.Replace(
"element is invalid - The value '' is invalid according to its datatype 'requiredString' - The actual length is less than the MinLength value.",
"cannot be blank.").Replace(
"is invalid according to its datatype 'size' - The Pattern constraint failed.",
"must be numeric.").Replace("element is invalid", "is invalid.");
errors.Add(new XElement("Error", message));
});
}
// If there were errors return them, otherwise return null
return errors.Elements().Count() > 0 ? errors : null;
}
请改为尝试:
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
static class Extensions
{
public static XElement ValidateXsd(this XElement source, string xsdPath)
{
var errors = new XElement("Errors");
// Reference: http://msdn.microsoft.com/en-us/library/bb358456.aspx
var schemas = new XmlSchemaSet();
using (var reader = XmlReader.Create(xsdPath))
{
schemas.Add("", reader);
}
var sourceQualifiedName = new XmlQualifiedName(source.Name.LocalName, source.Name.NamespaceName);
source.Validate(
schemas.GlobalElements[sourceQualifiedName],
schemas,
// Validation Event/Error Handling
(sender, e) =>
{
var message =
e.Message.Replace(
"element is invalid - The value '' is invalid according to its datatype 'requiredString' - The actual length is less than the MinLength value.",
"cannot be blank.").Replace(
"is invalid according to its datatype 'size' - The Pattern constraint failed.",
"must be numeric.").Replace("element is invalid", "is invalid.");
errors.Add(new XElement("Error", message));
});
// If there were errors return them, otherwise return null
return errors.Elements().Count() > 0 ? errors : null;
}
}