Validate xml file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can I validate an XmlDocument against an xsd file before it is saved to a
file? Any sample code?
 
I can't see any validated writer code, but you could perhaps use a
reader to parse the XmlDocument? Something like (untested - concept
only):

XmlDocument doc = null; // TODO: something else
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("urn:my-schema", "books.xsd"); //
TODO: your schema
XmlReader reader = XmlReader.Create(new
XmlNodeReader(doc.DocumentElement), settings);
reader.MoveToContent(); // unsure if needed
reader.Skip(); // according to MSDN performs subtree
validation

Marc
 
May also need:

settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += SomeEventHandler;
 
Hi,
The above code works but it comes out once it encounters the first error,
what should I do if I want to accumalate all the vaildation errors. I
appreciate for your answer

Thank u
Kaladhar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top