Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

schema validation XML

Tags:

c#

xml

schema

xsd

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 XmlDocument class, but it needs an event handler and all I need is true or false.

P.S. I'm working inVisual Studio 2010.

like image 495
aharon Avatar asked Jan 03 '11 11:01

aharon


1 Answers

There is a much easier 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 write this in VS so there might be a word that does not have the right case sensitivity, but this code works!

like image 54
aharon Avatar answered Sep 28 '22 08:09

aharon