Click here to Skip to main content
15,892,298 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;

            try
            {
                //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.Schemas.Add(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="http://www.w3.org/2001/XMLSchema" xmlns="CRS_Test.dtd" xmlns:wmh="http://www.wmhelp.com/2003/eGenerator" elementformdefault="qualified" targetnamespace="CRS_Test.dtd">
  <xs:element name="TEST">
    <xs:complextype />
  </xs:element>
</xs:schema>

my XML File
<test> </test>


Thanks in Advance.
Posted
Updated 27-Jun-11 5:14am
v5
Comments
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
 
Comments
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
 
Comments
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    
Hi,

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