It'sMe
It'sMe

Reputation: 99

Map Object Model to XML

I am working on a generation of PDF based on a ASP.Net MVC form, the datamodel that I am passing to the Controller is a complex object and I need to map it to XML and then generate a PDF from it. The problem is that I don't know what is the best way to map the object to XML. My code is in C# by the way. I will appreciate any sugestions.

Upvotes: 0

Views: 688

Answers (1)

Dan Field
Dan Field

Reputation: 21661

This is really bordering on overly broad, but here are some options that are built into .NET:

  • Use XmlSerializer from System.Xml.Serialization: Mark your class(es) as [Serializable], use attributes to specify node names and mappings. You could implement IXmlSerializable on your class as well to further control and customize how the XML would be read or written.
  • XDocument from System.Xml.Linq: You may find this interface easier to use if you don't need to serialize the entire class, and/or your class structure has little resemblance to the XML structure you're looking to create and you don't want to/can't modify the class(es). Just be aware that this will require updates if your classes change, whereas XmlSerializer will more easily incorporate changes to the class (unless you override ReadXml and WriteXml, in which case you're in the same boat).

Upvotes: 4

Related Questions