使用XSD模式验证XML数据
2012-03-01 01:18
337 查看
1.使用XSD模式验证XML数据
验证XML文档的步骤
(1)定义一个ValidationEventHandle事件处理方法。
(2)创建XmlReaderSettings对象的实例。
(3)将ValidationEventHandle与XmlReaderSettings相关联
(4) 将XmlReaderSettings的ValidationType属性设置为ValidationType.Schema.
(5)通过XmlReaderSettings类的属性将XSD模式添加至XmlReaderSettings类
(6)在解析XML数据的时候,XMLReader类使用Read方法验证XML文档。
Xml
XSD
View Code
2. 模式缓存 C#代码不同
C#代码
验证XML文档的步骤
(1)定义一个ValidationEventHandle事件处理方法。
(2)创建XmlReaderSettings对象的实例。
(3)将ValidationEventHandle与XmlReaderSettings相关联
(4) 将XmlReaderSettings的ValidationType属性设置为ValidationType.Schema.
(5)通过XmlReaderSettings类的属性将XSD模式添加至XmlReaderSettings类
(6)在解析XML数据的时候,XMLReader类使用Read方法验证XML文档。
Xml
<?xml version="1.0" encoding="utf-8" ?> <authors> <author> <au_id>123123</au_id> <au_lname>White</au_lname> <au_fname>Johnson</au_fname> <phone>23446345</phone> <address>10932 Bigge Rd.</address> <city>Menlo Park</city> <state>CA</state> <zip>049366</zip> <contract>true</contract> </author> <author> <au_id>123123</au_id> <au_lname>Green</au_lname> <au_fname>Johnson</au_fname> <phone>23446345</phone> <address>10932 Bigge Rd.</address> <city>Menlo Park</city> <state>CA</state> <zip>049366</zip> <contract>true</contract> </author> </authors>
XSD
View Code
private StringBuilder _builder = new StringBuilder(); protected void Page_Load(object sender, EventArgs e) { string xmlPath = Request.PhysicalApplicationPath + @"\App_Data\Authors.xml"; string xsdPath = Request.PhysicalApplicationPath + @"\App_Data\Authors.xsd"; XmlReader reader = null; XmlReaderSettings setting = new XmlReaderSettings(); setting.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(setting_ValidationEventHandler); setting.ValidationType = ValidationType.Schema; setting.Schemas.Add(null,XmlReader.Create(xsdPath)); reader = XmlReader.Create(xmlPath,setting); while (reader.Read()) { } if (_builder.ToString() == string.Empty) { Response.Write("Validation completed successfully."); } else Response.Write("Validation Faild.<br>" + _builder.ToString()); } void setting_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e) { _builder.Append("Validation error :"+e.Message+"<br>"); Response.Write(_builder.ToString()); }
2. 模式缓存 C#代码不同
C#代码
private StringBuilder _builder = new StringBuilder(); protected void Page_Load(object sender, EventArgs e) { string xmlPath = Request.PhysicalApplicationPath + @"\App_Data\Authors.xml"; string xsdPath = Request.PhysicalApplicationPath + @"\App_Data\Authors.xsd"; XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.Add(null,xsdPath); XmlReader reader = null; XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler); settings.ValidationType = ValidationType.Schema; settings.Schemas = schemaSet; reader = XmlReader.Create(xmlPath,settings); while (reader.Read()) { } if (_builder.ToString() == string.Empty) Response.Write("Validation completed successfully"); else Response.Write("Validation faild.<Br>"+_builder.ToString()); } void settings_ValidationEventHandler(object sender, ValidationEventArgs e) { _builder.Append("Validation error: "+e.Message+"<br>"); }
相关文章推荐
- XML校验学习之一:使用XSD模式验证XML数据
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
- 使用架构(XSD)验证XML文件
- [代码]使用XSD验证XML示例1(LINQ to XML)
- [代码]使用XSD验证XML示例(LINQ to XML)(转)
- 使用正则表达式进行xml数据验证
- 使用架构(XSD)验证XML文件
- java中使用xsd验证xml
- [代码]使用XSD验证XML示例2(LINQ to XML)
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
- [Step By Step]使用SAP Business Objects Data Services将XML数据导入到SAP HANA中(XML XSD Schema)
- JAVA使用JAXB框架根据schema.xsd文件验证XML合法性
- 使用 XmlReader 验证 XML 数据 import include
- 使用正则表达式进行xml数据验证
- Java使用Schema模式对XML验证
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
- 使用XSD验证xml
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
- 使用xsd文件验证xml
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式