C# XSD校验XML文件的代码
/// <summary> /// XSD文件校验XML /// </summary> /// <returns></returns> public ArrayList ECSValueValidation() { ArrayList al = new ArrayList(); try { //doc是XmlDocument对象 doc.Load(m_xmlURL); } catch (System.Exception ex) { return null; } //声明XmlSchema XmlSchemaSet schemas = new XmlSchemaSet(); schemas.Add("", XmlReader.Create(this.m_xsdPath)); //声明事件处理方法 ValidationEventHandler eventHandler = new ValidationEventHandler(delegate(object sender, ValidationEventArgs e) { switch (e.Severity) { case XmlSeverityType.Error: //error += e.Message; al.Add(e.Message); break; case XmlSeverityType.Warning: break; } }); doc.Schemas = schemas; //验证xml doc.Validate(eventHandler); //检查是否有异常 如果格式不正确就抛出来 //if (!"".Equals(error)) //{ // throw new Exception(error); // flag = false; //} return al; }