Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
235 views
Welcome To Ask or Share your Answers For Others

1 Answer

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:he 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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...