views:

39

answers:

1

I have a schema, and I have a document. I want to allow insertion of an xml fragment into that document, but, it must be valid according to the schema.

Is it possible to do this in C#?

A: 

I've never done this before, but you could use the NodeChanged, NodeInserted and NodeRemoved events of the XmlDocument (see http://msdn.microsoft.com/en-us/library/system.xml.xmldocument_events.aspx). When the event is raised, you could re-validate the document. If the validation fails, you could then un-change, remove, or re-insert the node.

You may also be able to use the XmlSchemaValidator class, that that would be more complicated. It follows a push-based model, and, during validation, basically tells you what would be valid at the current point. I suppose you could validate down to the point of insertion, then check to see of the node to be inserted would be valid at that point. I've only used this to generate XML documents conforming to a schema set, however; I've never used it for just plain validation.

John Saunders