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();
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();
}
}
}
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));
}
}
}
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相同