Click here to Skip to main content
15,912,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I've got an object consisting of a number of properties. I need to iterate through all the properties on the object, create a line of text for each property, consisting of its name and value, in order to then send them all in an email, e.g.

Name: Bob Todd
Role: Engineer
EmployeeNumber: 01919

...etc.

Converting the object to an XML representation is easy enough but I don't want the data in an XML format - the object represents a survey form that a user has filled in so the resulting email needs to plain text.

Any suggestions welcome! :)
Posted

That's what the ToString method is for, impliment it inside your class to return the data you want.
 
Share this answer
 
Comments
j4v1 25-Aug-10 16:52pm    
I tried a "ToString()" call on my object but all it returns is the object's name. Did you mean implementing a method which would build the string from the properties manually? That's what I wanted to avoid really - there's up to about 30 properties and I've got several survey forms to implement this for.
AspDotNetDev 25-Aug-10 17:13pm    
FYI, the OP didn't ask how to use ToString. The question was how to perform the actual serialization. There is nothing that says this needs to be part of the ToString implementation (nor should it, just as XML serialization isn't part of the default ToString implementation).
I can think of 4 options off the top of my head...

You could use reflection to automatically gather all the property names and values and output them to a string in the format you indicated. You could also decorate the properties with an attribute to indicate you don't want that particular property serialized.

Or you could go the easy route and manually do it (i.e., with a StringBuilder or String.Format), assuming you know the properties on the object at code time.

Or you could serialize the object to XML, then load it up in an XmlDocument, then go through the nodes and grab out the property names and values then output them to a string in the format you indicated.

Or you could output to JSON (.Net has some support for this) then use a string.Replace to get rid of any curly braces and commas (and maybe toss in a few newline characters).
 
Share this answer
 
Comments
j4v1 25-Aug-10 16:54pm    
You say use reflection, that sounds reasonable. I'm guessing I could create a Dictionary from the object and iterate through the name/value pairs?

As I explained to Christian Graus (thanks both for the replies by the way), I have loads of properties to iterate through...
AspDotNetDev 25-Aug-10 17:10pm    
Yes, with reflection (and and JSON and the XmlDocument techniques), the property name/value gathering is automatic (i.e., when you add a property to the object, you do not need to change your serialization code). Also, you don't need to create a dictionary object... you can just serialize the properties as you go through them using your reflection code. If you want a more targetted serialization (i.e., you only want to serialize some of the properties) and you can't modify the object by adding attributes, you can provide a list of properties to look for (or a list of properties to exclude) and serialize based on that (using any of the three techniques, reflection or XmlDocument or JSON).
j4v1 25-Aug-10 17:47pm    
Thanks, I'll give that a try tomorrow - sounds like the right direction as I'll be able to tailor the output :)

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