schema validation XML

后端 未结 3 1295
野趣味
野趣味 2020-12-15 11:03

I have an XSD file and an XML file, how can I check if the XML is in the right schema like the XSD file?

i know there is an validate function in the XmlDocumen

3条回答
  •  爱一瞬间的悲伤
    2020-12-15 11:43

    there is a much easy way to do it:

    private void ValidationCallBack(object sender, ValidationEventArgs e)
    {  
        throw new Exception();
    }
    
    public bool validate(string sxml)
    {
        try
        {
            XmlDocument xmld=new XmlDocument ();
            xmld.LoadXml(sxml);
            xmld.Schemas.Add(null,@"c:\the file location");
            xmld.validate(ValidationCallBack);
            return true;
        }
        catch
        {
            return false;
        }
    }
    

    P.S : I didn't wrote this in VS so there might be word that not in case sensitive, but this codes works!

提交回复
热议问题