Question

I have written a code where it validates and xml file against xml schema file. During development and testing my xsd file was located and referenced from as C:\xschema.xsd. This is a exe app , I am not sure how I can includee this file in my visual studio project and reference insde of code. Also it will be included as part of deployment files.

Dim XsdFile as String = "c:\xschema.xsd"
Dim Settings As XmlReaderSettings = New XmlReaderSettings()
Settings.Schemas.Add("", XsdFile)
Was it helpful?

Solution

First add the file to your project using "Add" menu Then change the property "Copy to Output Directory" to "Copy if newer" - this will copy the file to the output directory during build time

and then you can access the file like this:

Dim XsdFile as String = "xschema.xsd"

OTHER TIPS

You can either 1) put a schema hint in your XML files using the xsi:noNamespaceLocation hint and set

Settings.ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.ProcessSchemaLocation 

2) Have your deployment store the installation path in the Registry for example and construct the path to the schema file during run-time.

3) Put the schema file in your project as "Embedded Resource" and read the schema using

System.Xml.Schema.XmlSchema.Read(stream, validationHandler)

and then add it to the Settings

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top