Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All ,
I am validating XML files against an XML schema for which I read that the following code needs to be written (using XmlReaderSettings class)


C#
XmlReaderSettings xmlSettings = new XmlReaderSettings();
xmlSettings.Schemas = new System.Xml.Schema.XmlSchemaSet();
xmlSettings.Schemas.Add("myNamespace", xsdUri);
xmlSettings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create(xmlUri, xmlSettings);
while (reader.Read()) ;

but what I don't understand is why should we validate xmls through the code ,when xmls can be validated in Visual studio by adding the XML schema namespace in the xml file we intend to validate ?

Thanks In Advance
Posted
Updated 14-May-12 20:45pm
v3
Comments
Nelek 14-Apr-12 13:42pm    
added code tags

1 solution

Well,I seem to have found a satisfactory reason for why validation through code is done .
Its for the simple reason that ,In certain cases we dont want the users of our schema to simply ignore the errors thrown by the schema during static validation (i.e validation in Visual studio by specifying schema namespace in the xml file ;and no extra validation through code exists).We want the users to strictly adhere to the schema therefore we add extra validation through the code ,which throws an exception whenever there is an error in the xml.Untill the user corrects the xml,he cannot proceed.
 
Share this answer
 

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