Click here to Skip to main content
15,843,144 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guru's

Am trying to write a validate function in C#.
Here is what am trying to do.
Am getting XML from Webservices and am validate against XSD schema file.
But some how i missed something in code,so its not validate.
Please throw me some light.

public static bool validateXml(string strXml, string strXsdFilePath)
            bool validity = false;

                //step 1: Read the XSD file and create xml schema object from it
                StreamReader xsdReader = new StreamReader(strXsdFilePath);
                XmlSchema schema = new XmlSchema();
                schema = XmlSchema.Read(xsdReader, new ValidationEventHandler(XSDValidationEventHandler));

                //step 2: Instantiate xml reader settings so that xml file
                //is actually read according to the configured settings
                XmlReaderSettings readerSettings = new XmlReaderSettings();
                readerSettings.ValidationType = ValidationType.Schema;
                readerSettings.ValidationEventHandler += new ValidationEventHandler(XMLValidationEventHandler);

                //step 3: Read and validate xml file
                StringReader xmlReader = new StringReader(strXml);
                XmlReader objXmlReader = XmlReader.Create(xmlReader, readerSettings);
                while (objXmlReader.Read())
                { }

                //if at this point, then xml has been validated against the xsd file
                validity = true;
                //log the success
                log.Info("Successfule xml file validation");
            catch (Exception error)
                validity = false;
                // XML Validation failed
                log.Error( "XML validation failed:- ERROR MESSAGE: " + error.Message);

            return validity;

My XSD Schema File
<xs:schema xmlns:xs="" xmlns="CRS_Test.dtd" xmlns:wmh="" elementformdefault="qualified" targetnamespace="CRS_Test.dtd">
  <xs:element name="TEST">
    <xs:complextype />

my XML File
<test> </test>

Thanks in Advance.
Updated 27-Jun-11 6:14am
Morl99 27-Jun-11 9:54am    
Please tell us in more detail where your problem is. Error message?
shan1395 27-Jun-11 11:12am    
hi Morl99,

Its not throwing anything or showing any msg to say its validated or not.

The obvious issue here is that test does not match TEST. In your schema, you've defined an element of TEST, but your XML file says test. XML is case sensitive, so the two do not match.
Share this answer
shan1395 27-Jun-11 11:11am    
hi Pete,

thanks for your response,sorry i noticed and try to update <test> BUT COULDN'T update the xml file here.

but my xml file match with xsd file,with case sensitive
First create the proxy class from your XSD
for this opern VS command prompt and type follwoing comamd
xsd.exe a.xsd /c

where a.xsd is path for your xsd file and /c stands for proxy class language C#.

Now you can maap and validate your XML file againts proxy class.
Share this answer
shan1395 27-Jun-11 21:21pm    
is this anyway to catch the exception in the code ? bcoz its not throwing anything
shan1395 28-Jun-11 0:46am    

I was trying to validate my XML and Schema thru tool, i gotta below error message

cvc-elt.1: Cannot find the declaration of element 'CRS'.

Any Idea ?.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900