1

I have a list of objects with multiple properties that need to be assigned.

Although I doubt the source of the data is important, I'll include it for background purposes. I'm reading a list of attributes from select nodes in an XmlDocument using a foreach loop. I've created a loop iterator i and I'm trying to assign the values to properties in each object based on the attribute.Name and attribute.Value.

  // Reads each attribute: label, type, number etc...
  foreach (XmlNode node in myValues.SelectNodes("//Properties"))
  {
    foreach (XmlAttribute attribute in node.Attributes)
    {

      //this iterates through the attributes just fine
      //and in each iteration attribute.Name = the property
      //I want to set and attribute.Value = the value to set it to.

      myList[i].label = attribute.Value; //works

      //what I'd like to do something like
      myList[i].attribute.Name = attribute.Value;

It's driving me nuts. There must be someway to reference the value of attribute.Name?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • You could use reflection, but what it sounds like you really need to do is deserialize XML into a POCO: http://stackoverflow.com/questions/10518372/how-to-deserialize-xml-to-object – Nate Barbettini Jun 05 '16 at 17:57

3 Answers3

3

You can use reflection to set a property by its name, e.g:

var current = myList[i];
current.GetType().GetProperty(attribute.Name).SetValue(current, attribute.Value);
M4N
  • 94,805
  • 45
  • 217
  • 260
1

You need first to find the object where you want to update the properties in your loop. You could use LINQ as already answered multiple times @SO like in this post:

Using LINQ, how do I find an object with a given property value from a List?

   Question question14 = _mQuestions.FirstOrDefault(q => q.QuestionID == 14);
   if (question14 != null)
       question14.QuestionAnswer = "Some Text";

Then you could update the properties...

Community
  • 1
  • 1
STORM
  • 4,005
  • 11
  • 49
  • 98
1

You can deserialize the XML document into a POCO object. I personally like this approach over reflection, it gives me more control over what is happening and easier to see.

The biggest problem would be creating the POCO object(s) to deserialize into.

A cool feature of Visual Studio (this is 2015, not sure when it was introduced), you can 'Paste XML as classes'

  1. Copy the target XML document into you copy buffer (ctrl+c)

Image showing the Visual Studio Menu option of 'Paste XML as Classes' in the Edit menu's Paste Special list.

  1. Open a new class file in Visual Studio.
  2. Inside of the namespace but not he class (this will create class(es) put your curser.
  3. Use the menu to Edit-> Paste Special -> Paste XML as classes

Then you should see the classes generated by Visual Studio. NOTE: I have found that when it sees a number, if the numbers are under 255 it will create the field as a byte. I have had to come in afterwords and change those to ints. It is a quick find and replace but an important step if you run into deserialization issues.

Once you have the classes generated it is a simple matter of deserializing, here is some sample code:

//Object to deserialize into
MySerializableClass myObject;

//Create deserializer for object
XmlSerializer mySerializer = new XmlSerializer(typeof(MySerializableClass));

//Open XML file to read
using (FileStream myFileStream = new FileStream("myFileName.xml", FileMode.Open))
{
  //Deserialize XML into your POCO object
  myObject = (MySerializableClass) mySerializer.Deserialize(myFileStream);
}

Then you can use the object and get the values you want.

SBurris
  • 7,378
  • 5
  • 28
  • 36