检验多个xsd的xml是否合法

摘要:
文件Exists(schemaFileLocation))returnfalse;//重置错误/警告集合Errors=newList<string>();警告=newList<string>();XmlSchemaschema;使用(varfs=File.OpenRead(schemaFileLocation)){schema=XmlSchema.Read(fs,ValidationEventHandler);}variasValid=!错误。任意()&&!警告。Any();if(isValid){Schemas.Add(schema);}returnisValid;}//////对指定的XML文档执行XSD验证//////要验证的文件的完整文件路径///如果XML文件符合架构,则为True,否则为默认值publicboolIsValid(字符串xmlLocation){if(!File.Exists(xmlLocation)){thrownewFileNotFoundException(“指定的XML文件不存在”,xmlLocation);}使用(varxmlStream=File.OpenRead(xmlLocation)){returnIsValid(xmlStream);}}//////对所提供的XML流执行XSD验证//////<paramname=“xmlStream”>要验证的XML流///<return>True是XMLstreamconformstothechemas,否则默认˂/return>privateboolIsValid(StreamxmlStream){//重置错误/警告集合错误=newList<string>();警告=newList>string>(();varsettings=newXmlReaderSettings{ValidationType=ValidationType.Schema};设置。ValidationEventHandler+=ValidationEventHandler;foreach(varxmlSchemainSchemas){settings.Schemas.Add(xmlSchema);}varxmlFile=XmlReader。创建(xmlStream,设置);try{while(xmlFile.Read()){}}catch(XmlExceptionxex){Errors.Add(xex.Message);}回来错误.Any()&&!

Java - 使用 XSD 校验 XML

https://www.cnblogs.com/huey/p/4600817.html

这种方法不支持多个xsd文件,会报错

检验多个xsd的xml是否合法第1张

可以使用XMLBeans Tools来验证,3.1的版本用起来有问题,后来用2.6版本的就OK了

利用xmlbeans工具对xml格式进行验证(需要xsd文件)

https://blog.csdn.net/CronousGT/article/details/64137277

https://download.csdn.net/download/cronousgt/9787716#comment

http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html

检验多个xsd的xml是否合法第2张

为了解决这个问题我们需要使用LSResourceResolver, SchemaFactory在解析shcema的时候可以使用LSResourceResolver加载外部资源。

XML validation for multiple schemas 验证使用多个XSD schema的XML文件

https://blog.csdn.net/hld_hepeng/article/details/6318663

Validating XML against XSD schemas in C#

https://samjenkins.com/validating-xml-against-xsd/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Schema;

namespace KetoLibrary.Xml
{
    public class XsdValidator
    {
        public List<XmlSchema> Schemas { get; set; }
        public List<String> Errors { get; set; }
        public List<String> Warnings { get; set; }

        public XsdValidator()
        {
            Schemas = new List<XmlSchema>();
        }

        /// <summary>
        /// Add a schema to be used during the validation of the XML document
        /// </summary>
        /// <param name="schemaFileLocation">The file path for the XSD schema file to be added for validation</param>
        /// <returns>True if the schema file was successfully loaded, else false (if false, view Errors/Warnings for reason why)</returns>
        public bool AddSchema(string schemaFileLocation)
        {
            if (String.IsNullOrEmpty(schemaFileLocation)) return false;
            if (!File.Exists(schemaFileLocation)) return false;

            // Reset the Error/Warning collections
            Errors = new List<string>();
            Warnings = new List<string>();

            XmlSchema schema;

            using (var fs = File.OpenRead(schemaFileLocation))
            {
                schema = XmlSchema.Read(fs, ValidationEventHandler);
            }

            var isValid = !Errors.Any() && !Warnings.Any();

            if (isValid)
            {
                Schemas.Add(schema);
            }

            return isValid;
        }

        /// <summary>
        /// Perform the XSD validation against the specified XML document
        /// </summary>
        /// <param name="xmlLocation">The full file path of the file to be validated</param>
        /// <returns>True if the XML file conforms to the schemas, else false</returns>
        public bool IsValid(string xmlLocation)
        {
            if (!File.Exists(xmlLocation))
            {
                throw new FileNotFoundException("The specified XML file does not exist", xmlLocation);
            }

            using (var xmlStream = File.OpenRead(xmlLocation))
            {
                return IsValid(xmlStream);
            }
        }

        /// <summary>
        /// Perform the XSD validation against the supplied XML stream
        /// </summary>
        /// <param name="xmlStream">The XML stream to be validated</param>
        /// <returns>True is the XML stream conforms to the schemas, else false</returns>
        private bool IsValid(Stream xmlStream)
        {
            // Reset the Error/Warning collections
            Errors = new List<string>();
            Warnings = new List<string>();

            var settings = new XmlReaderSettings
            {
                ValidationType = ValidationType.Schema
            };
            settings.ValidationEventHandler += ValidationEventHandler;

            foreach (var xmlSchema in Schemas)
            {
                settings.Schemas.Add(xmlSchema);
            }

            var xmlFile = XmlReader.Create(xmlStream, settings);

            try
            {
                while (xmlFile.Read()) { }
            }
            catch (XmlException xex)
            {
                Errors.Add(xex.Message);
            }

            return !Errors.Any() && !Warnings.Any();
        }

        private void ValidationEventHandler(object sender, ValidationEventArgs e)
        {
            switch (e.Severity)
            {
                case XmlSeverityType.Error:
                    Errors.Add(e.Message);
                    break;
                case XmlSeverityType.Warning:
                    Warnings.Add(e.Message);
                    break;
            }
        }
    }
}

  

public void MultipleSchemas()
{
    var validator = new XsdValidator();
    validator.AddSchema(@"SchemaDoc1.xsd");
    validator.AddSchema(@"SchemaDoc2.xsd");
    var isValid = validator.IsValid(@"ValidXmlDoc1.xml");
}

  

免责声明:文章转载自《检验多个xsd的xml是否合法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇AsyncTaskhdparm 命令使用;关闭硬盘 HDD cache; 硬盘读写性能测试;下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

js读取解析JSON类型数据(转)

谢谢博主,转自http://blog.csdn.net/beyond0851/article/details/9285771 一、什么是JSON? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式, 是理想的数据交换格式,同时,JSON是 JavaScript 原生格式。 非常适合于...

Spring batch学习 (1)

          Spring Batch 批处理框架 埃森哲和Spring Source研发                          主要解决批处理数据的问题,包含并行处理,事务处理机制等。具有健壮性 可扩展,和自带的监控功能,并且支持断点和重发。让程序员更加注重于业务实现。           Spring Batch 结构如下      ...

notepad++添加自定义语言

步骤: 1.下载用户自定义语言XML文件 notepad++用户自定义语言XML文件下载 这里以jQuery为例,下载地址为:http://www.jamesallardice.com/downloads/notepadpp/jquery.zip 2.解压缩得到jquery.xml 和 userDefineLang_jQuery.xml两个文件。(注:若打...

XStream xml 解析框架使用笔记

1. xml的标签可以映射为类、类成员变量 2. 有子标签的标签映射为类,没有子标签的便签映射为类成员变量 3. 类名、类成员变量名如与标签名不一致需要通过注解或代码设置别名 1 //类名 2 @XStreamAlias("Info") 3 xStream.aliasType("Info", YLTextInfo.class); 4 //类成员变量名 5...

java 读取 yaml 文件

做 java 项目用的最多的配置文件就是 properites 或者 xml, xml 确实是被用烂了,Struts, Spring, Hibernate(ssh) 无一不用到 xml。相比厚重的 xml, properites 要清爽许多,一般的项目自己需要的配置也足够使用。但 properties 只支持 key=value 这种形式的配置,如果再遇...

gradle初体验

1、下载 下载网站 https://services.gradle.org/distributions/ 我们公司用的是4.8.1的版本 下载完成解压之后是这样的 2、配置环境变量 1、环境变量 ---> 新建 ---> 变量名GRADLE_HOME、变量值(自己的解压路径) 2、Path ---> 新建 ---> %GRAD...