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


    class ValidateXML
        {
            public string ErrString = string.Empty;
            public void ValidationEventCallBack(Object sender, ValidationEventArgs e)
            {
                if (e.Severity == XmlSeverityType.Warning)//区分是警告还是错误 
                {
                    //Console.WriteLine("验证成功!警告:" + e.Message);
                    ErrString += "验证成功!警告:" + e.Message;
                }
                else
                {
                   // Console.WriteLine("验证失败");
                    ErrString += "Err:" + e.Message;
                }
            }
    
            public void CheckXmlValidate(string strRequestXML)
            {
                //string ErrString = string.Empty;
                StringReader sRead = null;
                XmlReader xmlRead = null;
                XmlSchemaSet schemaSet;
    
                try
                {
                    schemaSet = new XmlSchemaSet();
    
                    sRead = new StringReader(strRequestXML);
    
                    schemaSet.Add(null, @"MySchema.xsd");
    
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.ValidationEventHandler += new ValidationEventHandler(this.ValidationEventCallBack);
                    settings.ValidationType = ValidationType.Schema;
                    settings.Schemas = schemaSet;
    
                    xmlRead = XmlReader.Create(sRead, settings);
                    while (xmlRead.Read())
                    {
    
                    }
    
                    if (ErrString.ToString() == String.Empty)
                    {
    
                        Console.WriteLine("验证成功!");
                    }
                    else
                    {
                        Console.WriteLine("验证失败!原因可能是:" + ErrString);
                    }
                }
                catch (XmlException exec)
                {
                    Console.WriteLine(exec.Message);
                }
                finally
                {
    
                    if (xmlRead != null)
                    {
    
                        xmlRead.Close();
                    }
                }
            }
        }
    
    public static void Main(string[] args)
            {
    ValidateXML vx = new ValidateXML();
                //StreamReader sr = new StreamReader(new FileStream(@"test.xml", FileMode.Open));
                vx.CheckXmlValidate(File.ReadAllText(@"test.xml"));
    
                PressQtoQuit();
            }
    
    public static void PressQtoQuit()
            {
                Console.WriteLine("Hit Q to exit");
                ConsoleKey key;
                do
                {
                    key = Console.ReadKey().Key;
                } while (key != ConsoleKey.Q);
            }
  • 相关阅读:
    浙江省CIO协会钱塘江论坛近日在网易云创沙龙宣布成立
    用Python解析XMind
    Flask写web时cookie的处理
    一篇文章看懂Facebook和新浪微博的智能FEED
    改进网易云音乐的“音乐社交”构想
    移动端爬虫工具与方法介绍
    用供应链管理思路降低教培产品成本
    【网易严选】iOS持续集成打包(Jenkins+fastlane+nginx)
    网易严选的wkwebview测试之路
    linux多进程之间的文件锁
  • 原文地址:https://www.cnblogs.com/joean/p/4982875.html
Copyright © 2020-2023  润新知