JBPM流程定义校验之.net利用XSD校验XML

JBPM流程定义校验之.net利用XSD校验XML

      上篇我们学习了在javascript中怎样利用XSD来验证xml,废话不在多说,今天我们来看一些怎样在.net中怎样实现利用xsd来校验xml!

     

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml;
using System.Xml.XPath;

namespace WFTH.XMLValidation
{
    
public class ValidationManager
    {
        
private static string error = string.Empty;
        
public static string ValidationXmlByXSD(string filePath,string xsdPath,string nameSpace)
        {
            XmlReaderSettings 
set = new XmlReaderSettings();
            //xml和xsd是单独的文件,否则是内联模式
            
if (!string.IsNullOrEmpty(xsdPath))
            {
                
set.Schemas.Add(nameSpace,xsdPath);
            }
            
set.ValidationType = ValidationType.Schema;
            
set.ValidationEventHandler += XSDValidationEventHandler;

            XmlReader reader 
= XmlReader.Create(filePath,set);

            
//XmlDocument doc = new XmlDocument();
            
//doc.Load(reader);
            
//XPathNavigator navigator = doc.CreateNavigator();


            
//XmlDocument doc = new XmlDocument();
            
//doc.Load(reader);

            
while (reader.Read()) ;

            
return error;
        }
  


        
private static void XSDValidationEventHandler(object sender, ValidationEventArgs e)
        {
            StringBuilder sb 
= new StringBuilder();
            
if (e.Severity == XmlSeverityType.Warning)
            {
                sb.Append(
"WARNING: ");
                sb.Append(e.Message);
            }
            
else if (e.Severity == XmlSeverityType.Error)
            {
                sb.Append(
"ERROR: ");
                sb.Append(e.Message);                
            }
            error
= sb.ToString();
        }


    }
}
复制代码

        客户端调用

       

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
using System.Xml.Schema;

namespace JavaScriptWA
{
    
public partial class _Default : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
//内联模式
            
//string xmlFilePath = Server.MapPath("a.xml");
            
//string xsdFilePath = null;
            
//string nameSpace =null;

            
//单独的xml和xsd
            string xmlFilePath=Server.MapPath("a.xml");
            
string xsdFilePath=Server.MapPath("note.xsd");
            
string nameSpace = "http://www.w3school.com.cn";
            Response.Write( WFTH.XMLValidation.ValidationManager.ValidationXmlByXSD(xmlFilePath, xsdFilePath, nameSpace));
        }
    }
}
复制代码

         xsd、xml文件与上篇JBPM流程定义校验之javascript利用XSD校验XML相同

posted @   无风听海  阅读(477)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示