验证大型XML文件 [英] Validating large Xml files

查看:432
本文介绍了验证大型XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为后续行动,以我的问题改造大型XML文件,我现在需要验证该模式。



我使用这个扩展的方法,它可以清楚地在,因为它的工作不正常或者



加以改进

 公共静态的XElement ValidateXsd(此的XElement源,串xsdPath)
{
VAR误差=新的XElement(错误);

//参考:http://msdn.microsoft.com/en-us/library/bb358456.aspx
VAR XSD = XDocument.Load(xsdPath);
变种的xml = XDocument.Load(source.CreateReader());

VAR模式=新的XmlSchemaSet();
schemas.Add(,xsd.CreateReader());

如果(xml.Document!= NULL)
{
xml.Document.Validate(架构,
//验证事件/错误处理
(发件人,E)=>
{
VAR消息= e.Message
.Replace(
元素是无效的 - 值''根据其数据类型是无效的requiredString ' - 实际长度小于MINLENGTH个值,
不能为空)
.Replace(
是无效的,根据其数据类型。'大小' - 该模式的约束失败,
必须是数字)
.Replace(
元素是无效的,
无效);

errors.Add(新的XElement(错误,消息));
}
);
}

//如果有错误回报他们,否则返回null
返回errors.Elements()计数()> 0?错误:空;
}


解决方案

试试这个:

 公共静态的XElement ValidateXsd(此的XElement源,串xsdPath)
{
VAR误差=新的XElement(错误);

//参考:http://msdn.microsoft.com/en-us/library/bb358456.aspx

VAR模式=新的XmlSchemaSet();使用
(VAR读卡器= XmlReader.Create(xsdPath))
{
schemas.Add(,读者);
}

{
source.Document.Validate(
模式,
//验证事件/错误处理
(发件人,E) = GT;
{
变种消息=
e.Message.Replace(
元素是无效的 - 值'requiredString'根据其数据类型是无效的 - 实际长度小于MINLENGTH个值,
不能为空)更换(
是无效的,根据其数据类型'大小' - 该模式约束失败,
。 必须是数字。)更换(元素是无效的,是无效的。);

errors.Add(新的XElement(错误,消息));
});
}

//如果有错误回报他们,否则返回null
返回errors.Elements()计数()> 0?错误:空;
}






试试这个:

 使用System.Linq的; 
使用的System.Xml;
使用System.Xml.Linq的;
使用System.Xml.Schema;

静态类扩展
{
公共静态的XElement ValidateXsd(此的XElement源,串xsdPath)
{
VAR误差=新的XElement(错误 );

//参考:http://msdn.microsoft.com/en-us/library/bb358456.aspx

VAR模式=新的XmlSchemaSet();使用
(VAR读卡器= XmlReader.Create(xsdPath))
{
schemas.Add(,读者);
}

变种sourceQualifiedName =新XmlQualifiedName(source.Name.LocalName,source.Name.NamespaceName);
source.Validate(
schemas.GlobalElements [sourceQualifiedName],
模式,
//验证事件/错误处理
(发件人,E)=>
{
变种消息=
e.Message.Replace(
元素是无效 - 值'requiredString'根据其数据类型是无效' - 实际长度小于。该模式约束失败,
必须是数字 - MINLENGTH个价值,
不能为空)更换(
,根据其数据类型'大小'是无效的。 ).Replace(元素是无效的,是无效的。);

errors.Add(新的XElement(错误,消息));
});

//如果有错误回报他们,否则返回null
返回errors.Elements()计数()> 0?错误:空;
}
}


As a follow up to my issue transforming large Xml files, I now need to validate the schema.

I was using this extension method, which can clearly be improved upon as it's not working correctly either

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;
}

解决方案

Try this instead:

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;
} 


Try this instead:

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;
        } 
    }

这篇关于验证大型XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆