I wants to set a xml document as a property, where user can set the values of xml attributes and tags but can not change the xml formate. user will not be allowed to delete or add any node in that xml.
Can any body help me out of this ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Blackbarbie BbPosted Sep 2, 2015, 8:16 AM
There are a couple of ways of doing this. If the data contained in the xml document can be translated into a serializable class (default constructor, getters and setters for all properties - since the data is xml anyway, it's probably trivially serializable) , you can simply expose the document like this:
This allows you to use the XmlSerializer class to load and save documents to disk, IO streams etc. without having to directly interact with the Xml DOM. Setting IsNullable to false means that when serialization takes place, a node will always be generated, or an exception thrown.
Otherwise, you can create a wrapper around the document, like so:
Either way, your aim is to hide the flexibility of the XML document from the user, and use an accessor class you have written to manipulate the elements you want to change.
If the document has an xsd, your task can be even easier - xsd.exe will allow you to automatically generate Seralizable C# classes from a given xsd schema