`

C# 实现XSD对XML的check处理

 
阅读更多

生命如果可以停止,我希望它永远的停留在那一刻

 

好了言归正传,用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>

 

 

0
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics