Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Windows service that will be listening for XML packets being sent over HTTP to the windows service. I want to create a class with properties based on the xml. let us assume I received the following xml:

XML
<persondetails>
   <name>abc</name>
   <surname>def</surname>
   <age>22</age>
</persondetails>



i want to be able to deserialize this xml to create a new class based on this information, or just deserialize this xml into an object where I can access the attributes.

The class needs to be created according to every xml received, since they will differ, so I can't create a class and just deserialize this to the typeof class.

I looked at xsd tool, but only found examples where working with this in the command prompt. I need it to be done automatically, without any user interaction.
Posted
Updated 23-Feb-12 21:50pm
v2
Comments
OriginalGriff 24-Feb-12 3:54am    
This may sound like a silly question, but if you autogenerate the class based on it's content, how are you going to use it in your code? Or are you trying to do something else with it?
SteveAdey 24-Feb-12 4:32am    
See my solution on how to dynamically create the object and use it in your code.

1 solution

I have used the ExpandoObject to do exactly this in a past project.

something like:

C#
dynamic personDetails = new ExpandoObject();

var personLookup = (IDictionary<string, object>)personDetails;
personLookup["Name"] = yourXmlValue;


I've excluded the XLinq here, but you should get the gist.

You can then pass the dynamic object around and just access it like regular properties:

i.e.
C#
var name = personDetails.Name;
 
Share this answer
 
v4
Comments
Andrew797 24-Feb-12 6:59am    
Xlinq? are you referring to linq to Xml?
SteveAdey 24-Feb-12 7:03am    
yes. But use whichever mechanism you are happy with.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900