Bilgehan
Bilgehan

Reputation: 305

How can I deserialize data from xml and how can I create classes dynamically by xml element name?

I have a dynamic XML structure. I mean, root name can be changed, element name can be changed and structure of xml can be changed.So How can I deserialize dynamic xml to objects?

Creating classes dynamically by xml element names, is this possible? If yes, then how can I do this?

Upvotes: 2

Views: 782

Answers (3)

Skizz
Skizz

Reputation: 71070

You want to look into the factory pattern. The basic idea is:

for each XML node, get the type name / name of node
  ask factory for type with given name
  create object of the given type
  pass the XML node to object to allow it to load its data

XSD schemas help here to verify the XML is structurally correct although not necessary. I did try this once, creating objects from XML, but I never found a way to have multiple schema files for one XML (this would have allowed me to extend the schema without changing the root schema, if you see what I mean).

Upvotes: 1

stuartd
stuartd

Reputation: 73243

If you're using .Net 4, the ExpandoObject might be what you need:

Represents an object whose members can be dynamically added and removed at run time.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

So How can I deserialize this type xml to objects?

Well, you cannot deserialize a XML without a specific XSD schema to statically typed classes. You could use a XDocument or XmlReader classes to parse and extract information from it.

Upvotes: 2

Related Questions