views:

427

answers:

4

Hi guys: I have to export my data, practically the entire domain model structure into a formal xml file. Does anyone know a visual tool that I can use to map an xml schema to my .NET object model?

A: 

The other way around is easy: you can use xsd.exe to generate .Net data-bound class models from XSD schemas.

But taking an arbitrary object model in .Net and serializing it into XML - I'm not even sure it's possible. Some of your types are bound to be more complex than what you can represent in XML. So starting from the XML side, it seems, is necessary.

Assaf Lavie
The domain model already exists.
Pita.O
I understand. But unlike the xsd->C# mapping, which is a solved problem, C#->xsd seems much more problematic, and I'm not sure you'll find a solution in that direction.
Assaf Lavie
There's no reason why there can't be. At design time: Reflection provides the interfaces, a mapping association is established.At runtime: an object/object is supplied and the same reflection does a getvalue on each PropertyInfo and builds the Xml.
Pita.O
A: 

I know of no support for such a scenario in the .NET framework.

I would start looking at reflection, looping over all types and their members, emitting corresponding descriptions in XML.

SealedSun
+1  A: 

The simplest way to export your objects to xml would be to mark each class with the <Serializalbe> attribute. Then call XmlSerializer.Serialize().

You can also import the xml back into your objects using XmlSerializer.Deserialize().

If you need a schema then you can use xsd.exe to generate it using either:

  1. The class library itself: xsd.exe myClassLibrary.dll
  2. The xml file generated by your call to XmlSerializer.Deserialize(): xsd.exe myFile.xml

See the documentation for xsd.exe and XmlSerializer for more detials.

Seth Reno
A: 

Others have pointed out the XSD support in visual studio. This has a visual designer, but while it will not map to your classes - it can create strongly typed objects.

XMLSerialization is great in a few cases: 1) Data transfer (web services) 2) To save time in prototypes. I strongly recommend against direct XML serialization of objects as a persistence mechanism (permanent storage). When you need to change your schema it will be painful. Yes, there are theoretical solutions, I've never seen them work easily in practice. I have felt a lot of pain maintaining (and re-architecting) quick serialization solutions and feel strongly about this :)

The question is why you are exporting the domain model? Is this an just an export functionality? If so, serialization is probably great. You might not find a visual tool to do this. But it's usually easy to create formal, even pretty, XML with attributes on the classes.

Precipitous