I’ve been looking around for this for quite some time and cannot seem to get anywhere. I need to find a way to accomplisht the following two tasks, based on a .Net Entity object:
- Create an XML file that contains the data in this entities, complete, with cascading nodes (representing foreign key relationships, basically). I’ve considered copying the data to a DataSet and then doing a WriteXml() on it, but this seems like an overkill. Entities must have a more direct way. Note, though, that I’m not trying to serialize the object. I just need the data in a simple XML file.
- Related to point 1, I need to also save an XSD representing the schema of this Entity, to go hand in hand with the XML mentioned above. Again, going through a DataSet and then WriteXmlSchema() is an option, but feels like overkill.
Is there anything in the Entity universe that can help do this in a simple way ?
Thx.
I think a combination of XML serialization and schema inference is probably all that you need.
The XmlSerializer should be good enough to generate the XML file that you’re looking for. You really don’t need to do anything besides pass in the stream or stream writer, and the instance of the root-level entity. The actual code for XML serialization is 2 lines:
I’m not sure why you want to avoid XML serialization.
In order to save the XSD relative to the set of entities, you can infer the schema from the generated XML. Schema inference is done by using the
XmlSchemaInferenceclass in conjunction withXmlSchemaSet.I usually create a small schema inference utility for myself wherever I go, it looks like this:
It doesn’t get any simpler than that.