用xsd验证xml

using System;
using System.Text;
using System.Xml;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        StringBuilder sb = null;
        protected void ValidateFile(object sender, EventArgs e)
        {
            string dataFile = Server.MapPath("Order.xml");
            string schemaFile = Server.MapPath("OrderSchema.xsd");
            string namespaceUrl = "http://tempuri.org/OrderSchema.xsd";

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas.Add(namespaceUrl, schemaFile);
            settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(settings_ValidationEventHandler);

            string errorMessage="这不是一个合乎规范的数据文件";
            sb = new StringBuilder();

            XmlReader reader = XmlReader.Create(dataFile, settings);
            try
            {
                reader.MoveToContent();
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Document && reader.NamespaceURI != namespaceUrl)
                    {
                        Response.Write(errorMessage);
                        break;
                    }
                }
            }
            catch (XmlException ex)
            {
                sb.AppendFormat("{0}<br />", ex.Message);
            }
            finally
            {
                reader.Close();
            }

            if (sb.Length == 0)
                Response.Write("该文档是合法的");
            else
                Response.Write(sb.ToString());
        }

        void settings_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e)
        {
            sb.AppendFormat("{0}<br />", e.Message);
        }
    }
}

如果传过来的是XMl的string类型

         StringBuilder sb = null;
        public bool CheckXML(string xml)
        {
            if (xml == null || xml == "")
            {
                return true;
            }
            byte[] array = Encoding.ASCII.GetBytes(xml);
            MemoryStream stream = new MemoryStream(array);

            string schemaFile = System.Web.HttpContext.Current.Server.MapPath("K2HandoffTemplate.xsd");

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas.Add(null, schemaFile);
            settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(settings_ValidationEventHandler);
            sb = new StringBuilder();

            XmlReader reader = XmlReader.Create(stream, settings);
            try
            {
                reader.MoveToContent();
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Document)
                    {
                        return false;
                    }
                }
            }
            catch (XmlException ex)
            {
                return false;
            }
            finally
            {
                reader.Close();
            }

            if (sb.Length == 0)
                return true;
            else
                return false;
        }

        void settings_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e)
        {
            sb.AppendFormat("{0}<br />", e.Message);
        }

 

posted @ 2014-12-01 15:51  小姐,请叫我“序员”好吗  阅读(143)  评论(0编辑  收藏  举报