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相同

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值