I have an XSD file and an XML file, how can I check if the XML is in the right schema like the XSD file?
i know there is an validate function in the XmlDocument
class, but it needs an event handler
and all I need is true or false.
P.S. I'm working inVisual Studio 2010.
You could do something like this.
The schema.
You can create a validating XmlReader instance using the XmlReaderSettings class and the Create method.
The above code will throw an exception if the schema is invalid or it is unable to deserialize the xml. rootType is the type of the root element in the equivalent class hierarchy.
Example : Schema at: XML Schema Tutorial. Save the file as
D:\SampleSchema.xsd
.Run
xsd.exe
:xsd.exe /c /out:D:\ "D:\SampleSchema.xsd"
/out
option is to specify the output directory,/c
is to specify the tool to generate classesD:\SampleSchema.cs
Add the class to the visual studio project.
For the above xsd sample, the root class is
note
.Call the method,
More info:
there is a much easy way to do it:
P.S : I didn't wrote this in VS so there might be word that not in case sensitive, but this codes works!