关于网友提出的“xsd 求通过xsd验证xml合法性的例子。要求有验证程序,xml和xsd文件,谢谢!”问题疑问,本网通过在网上对“xsd 求通过xsd验证xml合法性的例子。要求有验证程序,xml和xsd文件,谢谢!”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:xsd 求通过xsd验证xml合法性的例子。要求有验证程序,xml和xsd文件,谢谢!
描述: 如题
解决方案1: msdn上的例子:
booksSchema.xml
bookSchemaFail.xml
Benjamin
Franklin
The Confidence Man
Herman
Melville
11.99
The Gorgias
Plato
9.99
程序:
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
namespace SchemaData
{
///
/// Validator 的摘要说明。
///
public class Validator
{
private const String document3 = "booksSchema.xml";
private const String document4 = "booksSchemaFail.xml";
private const String document5 = "schema.xsd";
private XmlValidatingReader myXmlValidatingReader = null;
private XmlTextReader myXmlTextReader = null;
private Boolean Success = true;
private String[] args = {document3, document4, document5};
public Validator()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Run()
{
try
{
XmlSchemaCollection myXmlSchemaCollection = new XmlSchemaCollection();
myXmlSchemaCollection.Add("schema.xsd" , new XmlTextReader(args[2]));
// 用架构验证 XML 文件
Success = true;
Console.WriteLine();
Console.WriteLine("正在用架构文件 schema.xsd 验证 XML 文件 booksSchema.xml ...");
myXmlTextReader = new XmlTextReader (args[0]);
myXmlValidatingReader = new XmlValidatingReader(myXmlTextReader);
myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);
myXmlValidatingReader.ValidationType = ValidationType.Schema;
Validate();
// 架构验证失败
Success = true;
Console.WriteLine();
Console.WriteLine("正在用架构文件 schema.xsd 验证 XML 文件 booksSchemaFail.xml ...");
myXmlTextReader = new XmlTextReader (args[1]);
myXmlValidatingReader = new XmlValidatingReader(myXmlTextReader);
myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);
myXmlValidatingReader.ValidationType = ValidationType.Schema;
Validate();
}
catch (Exception e)
{
Console.WriteLine("异常:" + e.ToString());
}
finally
{
// 通过 XmlTextReader 完成
if (myXmlValidatingReader != null)
myXmlValidatingReader.Close();
}
}
private void Validate()
{
try
{
// 设置验证事件处理程序
myXmlValidatingReader.ValidationEventHandler += new ValidationEventHandler (this.ValidationEventHandle);
// 读取 XML 数据
while (myXmlValidatingReader.Read()){}
Console.WriteLine ("验证已完成。验证 {0}", (Success==true ? "成功" : "失败"));
}
catch (XmlException e)
{
Console.WriteLine ("Xml 异常:" + e.ToString());
}
catch (Exception e)
{
Console.WriteLine ("异常:" + e.ToString());
}
}
public void ValidationEventHandle (object sender, ValidationEventArgs args)
{
Success = false;
Console.WriteLine("\t验证错误:" + args.Message);
if (args.Severity == XmlSeverityType.Warning)
{
Console.WriteLine("未找到要强制验证的架构。");
}
else
if (args.Severity == XmlSeverityType.Error)
{
Console.WriteLine("验证实例文档时发生验证错误。");
}
if (args.Exception != null) // XSD 架构验证错误
{
Console.WriteLine(args.Exception.SourceUri + "," + args.Exception.LinePosition + "," + args.Exception.LineNumber);
}
//if (myXmlValidatingReader.Reader.LineNumber > 0)
//{
// Console.WriteLine("Line: "+ myXmlValidatingReader.Reader.LineNumber + " Position: " + myXmlValidatingReader.Reader.LinePosition);
//}
}
}
}
解决方案2: gz
以上介绍了“xsd 求通过xsd验证xml合法性的例子。要求有验证程序,xml和xsd文件,谢谢!”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3546655.html