How do I validate XML against schema during serialization

i know how to validate an xml file against a schema during deserialization. i have been searching the internet for an example of serialization validation and have found none. is this possible in c#? any help will be greatly appreciated.

Who is Participating?
redpipeConnect With a Mentor Commented:
I don't quite understand what you intend to do..?

If you already have an instantiated object and provide the correct classdef in the typeof parameter of the XmlSerializer method, then the conversion to a XMLified string should go without problems. (

Of course, if you use a generic serialization method (as shown here or if there is a mismatch between the ClassDef and XSD exceptions might/will occur.

Then a "generic" validation method of a XMLstring and corresponding XSDstring can be found at

Hope this helps, if not please provide more information.
I think xml schema validation occurs during serialization.
codechewerAuthor Commented:
I wanted to validate my xml document while in the  process of serializing the class into the xml document.

Though the classes were created from the schema, there are some conditions that made the serialized xml document not conform to the xsd structure and data-type requirement.

I did not want to finish serializing the class into an xml document and then attempt to re-read it into memory just for the purpose of  validating the document against the schema.

but I found out that you can only validate an xml document by reading the outputed xml document back into memory and there and then only can you validate the document against its schema to discover errors in the document if any.

Unless there is a smarter way of validating the xml string during the process of serializing the class into the xml file,  i will close this question and award the points to redpipe who pointed me to the only method i know for validating an xml document against its schema.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.