zoukankan      html  css  js  c++  java
  • C#中用schema验证xml的合法性

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using System.Xml;
    using System.Xml.Schema;
    using System.IO;
    
    namespace IETMAuthor
    {
        public partial class AssistantForm : DevExpress.XtraEditors.XtraForm
        {
            public AssistantForm()
            {
                InitializeComponent();
            }
    
            #region Private Variables & Constants           
            private const string PROCESSING = "处理正在进行中……";
            private const string READY = "尚未开始。";
            private const string FINISHED = "处理完成";
            private const string RIGHT = ",该步骤的处理返回了正确结果。";
            private const string WRONG = ",验证完成,但不是良好的XML文件";
            private const string VIEWDETAIL = "查看详细";
            string ErrString = "";
    
    
            private void AssistantForm_Load(object sender, EventArgs e)
            {
                this.dgvSteps.Rows.Clear();
    
                for (int intLoop = 1; intLoop <= 2; intLoop++)
                {
                    this.dgvSteps.Rows.Add(
                        IETMAuthor.Properties.Resources.PROCESS_READY,
                        intLoop.ToString(),
                        "" + intLoop.ToString() + "个步骤&" + READY,
                        string.Empty
                        );
                }
            }
    
            private void btnStart_Click(object sender, EventArgs e)
            {
                ErrString = "";
                this.dgvSteps.Rows.Clear();
    
                for (int intLoop = 1; intLoop <= 2; intLoop++)
                {
                    this.dgvSteps.Rows.Add(
                        IETMAuthor.Properties.Resources.PROCESS_READY,
                        intLoop.ToString(),
                        "" + intLoop.ToString() + "个步骤&" + READY,
                        string.Empty
                        );
                }
    
                string dataFile = buttonEdit1.Text;
    
                if (dataFile.Length <= 0)
                {
                    ErrString = "请选择要验证的XML文件";
                   
                    this.dgvSteps.Rows[0].Cells[1].Value = string.Empty;
                    this.dgvSteps.Rows[0].Cells[0].Value = IETMAuthor.Properties.Resources.PROCESS_WRONG;
                    this.dgvSteps.Rows[0].Cells[2].Value = WRONG;
                    this.dgvSteps.Rows[0].Cells[3].Value = VIEWDETAIL;
                    this.dgvSteps.Rows.Remove(this.dgvSteps.Rows[1]);            
                }
                else
                {
                    #region 验证
                    StringReader sRead = null;
                    XmlReader xmlRead = null;
                    XmlSchemaSet schemaSet;
    
                    try
                    {
                        schemaSet = new XmlSchemaSet();
    
                        sRead = new StringReader(File.ReadAllText(dataFile));
    
                        //schemaSet.Add(null, Application.StartupPath + "\xml\" + "schema.xsd");
                        schemaSet.Add(null, txtNamespaceUrl.Text);
    
                        XmlReaderSettings settings = new XmlReaderSettings();
                        settings.ProhibitDtd = false;
                        settings.ValidationEventHandler += new ValidationEventHandler(this.validationEventHandler);
                        settings.ValidationType = ValidationType.Schema;
                        settings.Schemas = schemaSet;
    
                        xmlRead = XmlReader.Create(sRead, settings);
                        while (xmlRead.Read())
                        {
    
                        }
    
                        if (ErrString.ToString() == String.Empty)
                        {
    
                           this.dgvSteps.Rows[1].Cells[2].Value = "验证成功,合规的良好的XML文件";
                           this.dgvSteps.Rows[1].Cells[0].Value = IETMAuthor.Properties.Resources.PROCESS_RIGHT;
                        }
                        else
                        {
    
                              this.dgvSteps.Rows[1].Cells[3].Value = VIEWDETAIL;
                              this.dgvSteps.Rows[1].Cells[2].Value = WRONG;
                              this.dgvSteps.Rows[1].Cells[0].Value = IETMAuthor.Properties.Resources.PROCESS_WRONG;
                          
                        }
                    }
                    catch (XmlException exec)
                    {
                         ErrString = exec.Message;
                         this.dgvSteps.Rows[1].Cells[3].Value = VIEWDETAIL;
                         this.dgvSteps.Rows[1].Cells[2].Value = WRONG;
                         this.dgvSteps.Rows[1].Cells[0].Value = IETMAuthor.Properties.Resources.PROCESS_WRONG;
                     
                    }
                    finally
                    {
                        if (xmlRead != null)
                        {
                            xmlRead.Close();
                        }
                    }
    
                    this.dgvSteps.Rows[0].Cells[2].Value = FINISHED;
                    this.dgvSteps.Rows[0].Cells[0].Value = IETMAuthor.Properties.Resources.PROCESS_RIGHT;
    
                    # endregion
                }
            }
    
            //exit the demo
            private void btnExit_Click(object sender, EventArgs e)
            {
                this.Dispose();
            }
    
            private void buttonEdit1_Click(object sender, EventArgs e)
            {
                this.ofd_xml.ShowDialog();
                this.buttonEdit1.Text = ofd_xml.FileName;
            }
    
            #endregion
    
    
            //when a step result in a failure, you may click the last cell of the step to see the detailed message
            private void dgvSteps_CellClick(object sender, DataGridViewCellEventArgs e)
            {  
    
                if (e.ColumnIndex == 3 &&
                    e.RowIndex >= 0 &&
                    this.dgvSteps.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == VIEWDETAIL)
                {
                    MessageBox.Show(
                        "" + System.Convert.ToString(e.RowIndex + 1) + "个步骤发生了错误:
    
    " + ErrString,
                        "提示",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
                }
            }
    
    
            //when any step is processing, you cannot close the form
            private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
            {
    
            }    
    
    
        
            void validationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e)
            {
                if (e.Severity == XmlSeverityType.Warning)//区分是警告还是错误 
                {
                    //Console.WriteLine("验证成功!警告:" + e.Message);
                    ErrString += "验证成功!警告:" + e.Message;
                }
                else
                {
                    // Console.WriteLine("验证失败");
                    ErrString += "Err:" + e.Message;
                }
            }
          
    
        }
    }

    验证器结果图:

  • 相关阅读:
    江西理工大学南昌校区cool code竞赛
    喵哈哈村的魔法考试 Round #3 (Div.2) ABCDE
    项目管理概要记录
    JS开发引用HTML DOM的location和document对象
    Linux下触摸屏驱动程序分析
    敦泰FT6X06单层自容调屏
    FT5X06 如何应用在10寸电容屏(linux-3.5电容屏驱动简析&移植10寸电容屏驱动到Android4.2) (by liukun321咕唧咕唧)
    基于FT5x06嵌入式Linux电容触摸屏驱动
    Linux/Android多点触摸协议
    高通 8x12 添加 TP和按键
  • 原文地址:https://www.cnblogs.com/jiangshuai52511/p/7722382.html
Copyright © 2011-2022 走看看