zoukankan      html  css  js  c++  java
  • 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相同

  • 相关阅读:
    机器视觉
    好心情
    什么是机器视觉?
    CY7C68013 USB接口相机开发记录
    CY7C68013 USB接口相机开发记录
    JVM调优总结(六)-新一代的垃圾回收算法
    JVM调优总结(五)-典型配置举例
    JVM调优总结(四)-分代垃圾回收详述
    JVM调优总结(三)-垃圾回收面临的问题
    JVM调优总结(二)-基本垃圾回收算法
  • 原文地址:https://www.cnblogs.com/wufengtinghai/p/2137667.html
Copyright © 2011-2022 走看看