生命如果可以停止,我希望它永远的停留在那一刻
好了言归正传,用C#实现一下XSD对XML的check处理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace XmlCheckMethod
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Xml.Schema;
namespace XmlCheckMethod
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void checkXSD_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "xsd|*.xsd|all file|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
xsdtxt.Text = openFileDialog.FileName;
}
}
private void button1_Click(object sender, EventArgs e)
{
ValidateXml(urltxt.Text, xsdtxt.Text);
}
public void ValidateXml(string xml, string spath)
{
XmlReaderSettings settings = new XmlReaderSettings();
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, spath);
settings.Schemas.Add(schemaSet);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
XmlReader reader = XmlReader.Create(xml, settings);
while (reader.Read()) ;
MessageBox.Show("***Validation Success");
Application.Exit();
}
private static void ValidationEventHandler(object sender, ValidationEventArgs args)
{
MessageBox.Show("***Validation error");
Application.Exit();
}
private void openXml_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "xml|*.xml|all file|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
urltxt.Text = openFileDialog.FileName;
}
}
}
}
namespace XmlCheckMethod
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.OpenXsd = new System.Windows.Forms.Button();
this.urltxt = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.xsdtxt = new System.Windows.Forms.TextBox();
this.openXml = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// OpenXsd
//
this.OpenXsd.Location = new System.Drawing.Point(268, 43);
this.OpenXsd.Name = "OpenXsd";
this.OpenXsd.Size = new System.Drawing.Size(75, 23);
this.OpenXsd.TabIndex = 0;
this.OpenXsd.Text = "OpenXsd";
this.OpenXsd.UseVisualStyleBackColor = true;
this.OpenXsd.Click += new System.EventHandler(this.checkXSD_Click);
//
// urltxt
//
this.urltxt.Location = new System.Drawing.Point(12, 12);
this.urltxt.Name = "urltxt";
this.urltxt.Size = new System.Drawing.Size(250, 19);
this.urltxt.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(364, 27);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(92, 23);
this.button1.TabIndex = 2;
this.button1.Text = "CheckValidation";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// xsdtxt
//
this.xsdtxt.Location = new System.Drawing.Point(12, 45);
this.xsdtxt.Name = "xsdtxt";
this.xsdtxt.Size = new System.Drawing.Size(250, 19);
this.xsdtxt.TabIndex = 3;
//
// openXml
//
this.openXml.Location = new System.Drawing.Point(268, 12);
this.openXml.Name = "openXml";
this.openXml.Size = new System.Drawing.Size(75, 23);
this.openXml.TabIndex = 4;
this.openXml.Text = "openXml";
this.openXml.UseVisualStyleBackColor = true;
this.openXml.Click += new System.EventHandler(this.openXml_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(477, 76);
this.Controls.Add(this.openXml);
this.Controls.Add(this.xsdtxt);
this.Controls.Add(this.button1);
this.Controls.Add(this.urltxt);
this.Controls.Add(this.OpenXsd);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "CheckXSD";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button OpenXsd;
private System.Windows.Forms.TextBox urltxt;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox xsdtxt;
private System.Windows.Forms.Button openXml;
}
}
<?xml version="1.0" encoding="utf-8" ?>
<Order>
<OrderID>10248</OrderID>
<OrderDate>2009-01-01</OrderDate>
<Details>
<OrderItem>
<ItemNumber>1</ItemNumber>
<ProductID>1</ProductID>
<Quantity>2</Quantity>
<UnitPrice>20</UnitPrice>
</OrderItem>
<OrderItem>
<ItemNumber>1</ItemNumber>
<ProductID>1</ProductID>
<Quantity>2</Quantity>
<UnitPrice>20</UnitPrice>
</OrderItem>
<OrderItem>
<ItemNumber>1</ItemNumber>
<ProductID>1</ProductID>
<Quantity>2</Quantity>
<UnitPrice>20</UnitPrice>
</OrderItem>
<OrderItem>
<ItemNumber>1</ItemNumber>
<ProductID>1</ProductID>
<Quantity>2</Quantity>
<UnitPrice>20</UnitPrice>
</OrderItem>
</Details>
</Order>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="OrderSchema"
targetNamespace="http://tempuri.org/OrderSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/OrderSchema.xsd"
xmlns:mstns="http://tempuri.org/OrderSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element name="OrderID" type="xs:integer"></xs:element>
<xs:element name="OrderDate" type="xs:date"></xs:element>
<xs:element name="Details">
<xs:complexType>
<xs:group ref="OrderItemGroup" minOccurs="1" maxOccurs="unbounded"></xs:group>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:group name="OrderItemGroup">
<xs:sequence>
<xs:element name="OrderItem">
<xs:complexType>
<xs:sequence>
<xs:element name="ItemNumber" type="xs:integer"></xs:element>
<xs:element name="ProductID" type="xs:integer"></xs:element>
<xs:element name="Quantity" type="xs:double"></xs:element>
<xs:element name="UnitPrice" type="xs:double"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:group>
</xs:schema>
分享到:
相关推荐
通过xsd产生C#实体类解析XML或生成XML,有疑问请私聊
C#雁阵xml是否满足xsd规则
在vs2005里,用vc实现xsd对xml的检测
通过xsd文件生成样例xml的工具,有完整源代码
java生成xsd,xml示例
xml 文件与xsd文件的转化以及xsd验证xml的合法性
xml生成xsd 使用方法:java -jar trang.jar EchoRequest.xml EchoRequest.xsd
XML验证器(XSD验证XML)附Delphi7源码 http://blog.csdn.net/akof1314/article/details/6860264 有介绍
程序都还能保证对XML的更改使其满足XSD中规定的元素和属性。 也可以试试其他的XML文件,关于产生XML对应的XSD文件,可以从这个网址上转换一下: http://www.freeformatter.com/xsd-generator.html 我的做法是: 1 ...
使用jaxb根据xsd生成xml文档,不要积分的,赶快下载
主要介绍了C#使用xsd文件验证XML格式是否正确的实现方法,结合实例形式分析了C#针对xml文件的创建、验证相关操作技巧,需要的朋友可以参考下
之前做一个开发项目,要校验xml报文合法性,根据Schema使用javax.xml.validation.Validator倒是能验证XML,但是要求是顺序一致才行,一直没找到合适的排序,就自己写了一个排序代码,根据xsd规范文件给xml排序,有...
castor解析xml与xsd校验xml
XML Schema语言也就是XML Schema Definition (XSD)。 XML Schema描述了XML文档的结构。可以用一个指定的XML Schema来验证某个XML文档,以检查该XML文档是否符合其要求。文档设计者可以通过XML Schema指定一个XML文档...
在vs2005里,使用c#语言完成xsd验证xml语法
验证xml文件与xsd是否匹配
xml文件转xsd文件,方便生成java类
XML Schema xsdXML Schema xsdXML Schema xsdXML Schema xsd
从XSD专程XML,非常实用。class CSaxContentHandler : public ISAXContentHandler { public: CSaxContentHandler(); virtual ~CSaxContentHandler(); virtual long __stdcall QueryInterface(const struct _...