simple xsd validation

Hello, I'm new to the System.Xml namespace  and I have the following problem: I have two strings representing an XML and an XSD.

What is the most simple way to validate the XML against the XSD Is there a way to avoid Streams, URIs and EventHandlers while doing this

Thank you in advance.

 



Answer this question

simple xsd validation

  • Jeff Cieslik

    Thank you, Arty. Your post was helpful.

    But what if I have the actual xml and xsd as strings

    For example:

    string xml = "< xml version='1.0' encoding='utf-8' ><a id='root element'>bla bla...</a>";

    string xsd = "<xs:schema id='blabla...'...";

    Is there a more simple way to do it


  • fadiz

    Thank you both for your replies.

    It seems that this is the most simple way to do it, and there's no way of avoiding streams and eventHandlers.


  • trggr_happy_jack

    Hello,

    You need to first load the schema's xml into a XmlSchema object... that will mean a reader and an event handler !! You'll need a StringReader to read the text.

    XmlSchema schema = XmlSchema.Read(reader, ValidationEventHandler)

    Once you read in the schema xml you need to compile it into a SOM. How you do that will depend on the .NET version your using. XmlSchemaSet compiles in .NET v2, schema.Compile() in .NET v1.0.

    Once you do that you can validate the xml document, again this will depend on the .NET framework version your using. In .NET v1.0 use XmlValidatingReader in .NET v2.0 you can use the XmlReader class with some settings (I think). In .NET v2.0 you can also validate using the XmlDocument using it's Schemas properties.

    Hopefully that will help.



  • kidnapt

  • simple xsd validation