asp.net xsd验证xml文件代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Schema;
namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //验证方法1
            Response.Write("<h1>方法第1种:</h1><br/>");
            Response.Write(returnValueFunction1());
            Response.Write("<hr/>");
            Response.Write("<h1>方法第2种:</h1><br/>");
            Response.Write(returnValueFunction2());
        }
        #region 验证方法第1种
        private string returnValueFunction1()
        {
            string Er = "";
            XmlReaderSettings settings = new XmlReaderSettings();
            //验证类型设置为模式(xsd)
            settings.ValidationType = ValidationType.Schema;
            //为XmlReaderSettings对象添加模式
            //第一个参数是targetNamespace的值,null表示采用XSD文件里targetNamespace属性的值
            //如果要明确传递此参数,务必与targetNamespace的值一致
            //第二个参数一定要采用绝对路径或物理路径,不能采用相对路径
            //settings.Schemas.Add("http://www.w3.org/2001/XMLSchem", Server.MapPath("test.xml"));
            settings.Schemas.Add(null, Server.MapPath("testSchema.xsd"));
            //同理第一个参数必须是绝对路径或物理路径
            XmlReader reader = XmlReader.Create(Server.MapPath("test.xml"), settings);
            try
            {
                while (reader.Read())
                {
                }
  Er = "OK";
            }
            catch (Exception e)
            {
                Er = e.Message;
            }
            finally
            {
                reader.Close();
            }
            return Er;
        }
        #endregion
        #region 验证方法第2种
        private string returnValueFunction2() 
        {
            string Function2Er = "";
            try
            {
                using (XmlTextReader xmltr = new XmlTextReader(Server.MapPath("test.xml")))
                {
                    //XmlValidatingReader已过时;可以使用 XmlReaderSettings 类和 Create 方法创建一个验证 XmlReader 实例
                    using (XmlValidatingReader xmlvr = new XmlValidatingReader(xmltr))
                    {
                        xmlvr.Schemas.Add(null, Server.MapPath("testSchema.xsd"));
                        while (xmlvr.Read())
                        {

                        }
                    }
                }
                Function2Er = "OK";
            }
            catch (Exception e)
            {
                Function2Er = e.Message;
            }
            return Function2Er;
        }
        #endregion
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值