Can XmlWriter validate against schema also? (.NET 2.0)
Hi,
I know that XmlReader can validate XML data against XSD. Is there an option to use an XmlWriter which can validate its data as it writes it?
[149 byte] By [
TechMan] at [2008-2-8]
The XmlWriter class doesn't seem to support validation. But if you have the XML stored in a XmlDocument prior to saving, you can use one of the XmlDocument.Validate() overloads.Side note: I do not see any reasonable way to handle validation errors when using a validating
XmlWriter class (e.g. what should happen if the last node in the document is invalid to the schema but the rest of the document has already been stored on disk?) so the lack of a validation support in this class seems intentional :]hth,
-- b.gr
In .NET 2.0 there is a new push model validator called System.Xml.Schema.XmlSchemaValidator with methods like ValidateElement(string name, string namespace,...) and ValidateAttribute(string name, string namespace, ...) which you could easily call from your own subclass of XmlWriter, it also has a handy method called GetExpectedParticles which tells you what is expected based on the current state.