xsd验证xml - 含源码
关键代码:
class XmlSchemaSetExample
{
private string logpath;
public void handle(String names, String xml, String xsd, String lpath)
{
this.logpath = lpath;
XmlReaderSettings booksSettings = new XmlReaderSettings();
booksSettings.Schemas.Add(names, xsd);
booksSettings.ValidationType = ValidationType.Schema;
booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler);
XmlReader books = XmlReader.Create(xml, booksSettings);
while (books.Read()) { }
}
private void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
{
LogManager.LogFielPrefix = "EADI ";
LogManager.LogPath = @logpath;
if (e.Severity == XmlSeverityType.Warning)
{
LogManager.WriteLog(LogFile.Warning, "WARNING: " + e.Message);
// MessageBox.Show("WARNING: " + e.Message);
// Console.Write("WARNING: ");
//Console.WriteLine(e.Message);
}
else if (e.Severity == XmlSeverityType.Error)
{
LogManager.WriteLog(LogFile.Error, "ERROR: " + e.Message);
// MessageBox.Show("ERROR: " + e.Message);
//Console.Write("ERROR: ");
//Console.WriteLine(e.Message);
}
}
}
调用方法:
XmlSchemaSetExample xmlSchemaSetExample = new XmlSchemaSetExample();
xmlSchemaSetExample.handle(textNameSpace.Text, path.ToString() + ".xml", path.ToString() + ".xsd", textlog.Text);
测试的XML和XSD(文件名字一定要相同)
contosoBooks.xml
<bookstore xmlns="http://www.contoso.com/books">
<book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99d</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99d</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
contosoBooks.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string" />
<xs:element minOccurs="0" name="first-name" type="xs:string" />
<xs:element minOccurs="0" name="last-name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="price" type="xs:decimal" />
</xs:sequence>
<xs:attribute name="genre" type="xs:string" use="required" />
<xs:attribute name="publicationdate" type="xs:date" use="required" />
<xs:attribute name="ISBN" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
详情请看附件...
分享到:
相关推荐
NULL 博文链接:https://zhoujianboy.iteye.com/blog/702889
NULL 博文链接:https://zhoujianboy.iteye.com/blog/816343
XML验证器(XSD验证XML)附Delphi7源码 http://blog.csdn.net/akof1314/article/details/6860264 有介绍
NULL 博文链接:https://nickfover.iteye.com/blog/2155663
ajax调用java实例源码xsd 表格 您想要制作一个提交结构化数据 (XML/JSON) 的 Web 表单。 xsd-forms基于已用一些表示信息注释的 xml 模式 (XSD) 为您生成 Web 表单。 xsd-forms已被部署到一个免费可用的环境中,以...
NULL 博文链接:https://wenhai.iteye.com/blog/563416
现在我们写一个程序来验证下: java 代码 1. package com.savage.aop; 2. 3. import org.springframework.context.ApplicationContext; 4. import org.springframework.context.support....
CreateAttributeElement 演示通过XElement创建具有属性的XML元素。 CreateSubElement 演示通过XElement创建具有子元素的XML元素。 CreateXMLTree 演示通过XElement创建复杂的完整的XML树。 ...
java源码X工厂 XML、XSL、XPATH 工具箱和批处理 Xfactory 是一个免费软件,我编写它只是为了使用 spring-boot 和 ZK 框架。 Xfactory 功能: 灵活且高性能的 xsl 批量转换 XSLT 工作台 XSD 验证器 XPath 工作台 XML ...
Pom.xml:配置文件示例(含的编译环境) <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=...
RapidWebDev框架源码 1. 概述 Eunge 在CodePlex上发表的视屏介绍 RapidWebDev是一个完全开源的企业应用快速开发框架,它提供了在.NET开发中经常会用到的组件和服务,当前包括了常用组件, 扩展模型, 业务平台和 ...
gSOAP编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现,从而让C/C++语言开发web服务或客户端程序的工作变得轻松了很多。绝大多数的C++web服务工具包提供一组API函数类库来处理特定的SOAP数据结构,这样就使得用户...
技术基础 New Folder 多样式星期名字转换 [Design, C#] .NET关于string转换的一个小Bug Regular Expressions 完整的在.net后台执行javascript脚本...Serialize Your Deck with Positron [XML Serialization, XSD, C#]...