using System; using System.Xml; using System.Xml.Schema; namespace MossListAuthorization { public class XmlChecker { public bool checkedSuccessful = true; /// <summary> /// 使用相应的schema来校验参数是否合法 /// </summary> /// <param name="xmlString"></param> public void CheckXml(XmlDocument doc) { try { doc.Validate(new ValidationEventHandler(MyCheckEventHandler)); } catch (Exception ex) { checkedSuccessful = false; throw ex; } } /// <summary> /// 校验xml的schema出现问题时调用的委托方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MyCheckEventHandler(object sender, ValidationEventArgs e) { if (e.Severity == XmlSeverityType.Warning || e.Severity == XmlSeverityType.Error) { checkedSuccessful = false; throw new XmlException(e.Message); } } } }