tags:

views:

19

answers:

2

New question about the previous question

Previous question was:

I have to populate a few objects that contains 30-50 property from a few linq to xml results

is there a way to populate these object without having to manually write every specific prop=value for every property?

a kind of

(from xml in myXml select xml).ToList<Object>;

in the select, is it possible to use reflection to assign value to my object based on the name of the xml field?

A: 

There is no way for you to avoid coping values explicitly.
If you have a large no of fields i would much rather hold on to the Xelement and read values from it as and when needed rather than convert it into objects

Vinay B R
A: 

If your class you're wanting to create instances of has consistent XML mappings, you could use one of the .NET serialization frameworks instead of or in conjunction with LINQ to XML. For example, if you were using data contract serialization, then given this class:

[DataContract]
public class MySerializableObject
{
    [DataMember]
    public string PropertyA { get; set; }

    [DataMember]
    public int PropertyB { get; set; }
}

You could have code such as this:

using (var someStream = OpenXmlStream())
{
    var deserializer = new DataContractSerializer(
        typeof(List<MySerializableObject>));
    var myList = deserializer.ReadObject(someStream) 
        as List<MySerializableObject>;
}
Jacob