Skip to main content

Posts

Showing posts with the label XSD Schema

Schema Import using XSD tool

We are all aware of XSD tool that comes along the Visual Studio 2005. We all might have used it to generate classes and dlls using this tool from xsd schemas. In order to use schemas that import other schemas using xs:import , we need to specify both the base schema and the imported schema on the command line.As the XSD tool doesn't seem to understand schemaLocation attribute . Just mentioning the import tag won't import the schema while creating classes. So the correct usage of xsd.exe would be xsd.exe ImportingSchema.xsd ImportedSchema.xsd {options}

XSD Schema Validation

The other day I was writing a .NET application which would read all the configuration details from an xml file.The application was not going to initialize properly unless the data in the config file were well formed.So I was wondering whether there was a way to validate an xml against some defined XSD schema. Thankfully there is. This is how I did it. ............................................. //Open the file with xml reader XmlTextReader reader = new XmlTextReader (configFilePath); //Some local path // Read the Schema XmlReader schemaReader = new XmlTextReader (schemaPath); //path of the XSD file XmlSchema schema = XmlSchema .Read(schemaReader , null); // Create a validating Reader for the text reader XmlValidatingReader validatingReader = new XmlValidatingReader (reader ); validatingReader.ValidationType = ValidationType.Schema; validatingReader.Schemas.Add(schema); // Add the Schema to validatingReader validatingReader.ValidationEventHandler += new ValidationEventHandler( t...