C#中用schema验证xml的合法性

  1. class ValidateXML  
  2.     {  
  3.         public string ErrString = string.Empty;  
  4.         public void ValidationEventCallBack(Object sender, ValidationEventArgs e)  
  5.         {  
  6.             if (e.Severity == XmlSeverityType.Warning)//区分是警告还是错误   
  7.             {  
  8.                 //Console.WriteLine("验证成功!警告:" + e.Message);  
  9.                 ErrString += "验证成功!警告:" + e.Message;  
  10.             }  
  11.             else  
  12.             {  
  13.                // Console.WriteLine("验证失败");  
  14.                 ErrString += "Err:" + e.Message;  
  15.             }  
  16.         }  
  17.   
  18.         public void CheckXmlValidate(string strRequestXML)  
  19.         {  
  20.             //string ErrString = string.Empty;  
  21.             StringReader sRead = null;  
  22.             XmlReader xmlRead = null;  
  23.             XmlSchemaSet schemaSet;  
  24.   
  25.             try  
  26.             {  
  27.                 schemaSet = new XmlSchemaSet();  
  28.   
  29.                 sRead = new StringReader(strRequestXML);  
  30.   
  31.                 schemaSet.Add(null, @"MySchema.xsd");  
  32.   
  33.                 XmlReaderSettings settings = new XmlReaderSettings();  
  34.                 settings.ValidationEventHandler += new ValidationEventHandler(this.ValidationEventCallBack);  
  35.                 settings.ValidationType = ValidationType.Schema;  
  36.                 settings.Schemas = schemaSet;  
  37.   
  38.                 xmlRead = XmlReader.Create(sRead, settings);  
  39.                 while (xmlRead.Read())  
  40.                 {  
  41.   
  42.                 }  
  43.   
  44.                 if (ErrString.ToString() == String.Empty)  
  45.                 {  
  46.   
  47.                     Console.WriteLine("验证成功!");  
  48.                 }  
  49.                 else  
  50.                 {  
  51.                     Console.WriteLine("验证失败!原因可能是:" + ErrString);  
  52.                 }  
  53.             }  
  54.             catch (XmlException exec)  
  55.             {  
  56.                 Console.WriteLine(exec.Message);  
  57.             }  
  58.             finally  
  59.             {  
  60.   
  61.                 if (xmlRead != null)  
  62.                 {  
  63.   
  64.                     xmlRead.Close();  
  65.                 }  
  66.             }  
  67.         }  
  68.     }  
  69.   
  70. public static void Main(string[] args)  
  71.         {  
  72. ValidateXML vx = new ValidateXML();  
  73.             //StreamReader sr = new StreamReader(new FileStream(@"test.xml", FileMode.Open));  
  74.             vx.CheckXmlValidate(File.ReadAllText(@"test.xml"));  
  75.   
  76.             PressQtoQuit();  
  77.         }  
  78.   
  79. public static void PressQtoQuit()  
  80.         {  
  81.             Console.WriteLine("Hit Q to exit");  
  82.             ConsoleKey key;  
  83.             do  
  84.             {  
  85.                 key = Console.ReadKey().Key;  
  86.             } while (key != ConsoleKey.Q);  
  87.         } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值