开发者

validate XML against XSD in asp.net4.0

开发者 https://www.devze.com 2023-04-03 11:17 出处:网络
I am using following code to validate XML against XSD in asp.net4.0. static int Erro开发者_C百科rsCount = 0;

I am using following code to validate XML against XSD in asp.net4.0.

 static int Erro开发者_C百科rsCount = 0;

    public static void ValidationHandler(object sender, ValidationEventArgs args)
    {
        //ErrorMessage = ErrorMessage + args.Message + "\r\n";
        ErrorsCount++;
    }

    public bool validate(String xml)
    {
        XmlTextReader tr = null;
        XmlSchemaCollection xsc = null;
        XmlValidatingReader vr = null;
        string XSDLocation = string.Empty;
        xsc = new XmlSchemaCollection();
        XSDLocation = "D:\\PENN Universal Gateway\\Penn.Common.Shared\\Schema\\s2sPlayer.xsd");

        tr = new XmlTextReader(XSDLocation);
        xsc.Add(null, tr);
        vr = new XmlValidatingReader(xml.ToString(), XmlNodeType.Document, null);
        vr.Schemas.Add(xsc);
        vr.ValidationType = ValidationType.Schema;
        vr.ValidationEventHandler -= new ValidationEventHandler(ValidationHandler);
        vr.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);

        while (vr.Read()) ;
        vr.Close();
        if (ErrorsCount > 0)
        {
            return false;

        }
        return true;
    }

this function always return me False thats is not valid for valid XML,XSD also.

plz suggest what is going wrong here...


It looks like your handler is always called, and all the handler does is increment the error count. So ErrorsCount will always be > 0 and your method will always return false.

Or am I missing something?

0

精彩评论

暂无评论...
验证码 换一张
取 消