C#使用xsd文件验证XML 格式是否正确
XmlReaderSettings st = new XmlReaderSettings(); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.Add(null, spath); //st.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler); st.ValidationFlags = st.ValidationFlags | XmlSchemaValidationFlags.ReportValidationWarnings; st.ValidationType = ValidationType.Schema; st.Schemas.Add(schemaSet, @"a.xsd"); //设置验证xml出错时的事件。 st.ValidationEventHandler += (obj, e)=>{ //这里加上你的信息处理。 }; XmlReader xr = XmlReader.Create(@"a.xml", st); while (xr.Read()) { if (xr.IsStartElement() { xr.Read(); } } xr.Close();
,温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/68254.html