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) + "个步骤发生了错误:\r\n\r\n" + 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; } } } }
验证器结果图: