Convert string to variable name

I have an XML file, I have a node, and I read all the child nodes. The name childNode corresponds to the variable that I have to set with the value of this childNode.

In a loop, I would like to set:

  • myvar1 to MyValue1
  • myvar2 to MyValue2

C # code:

protected string myvar1; protected string myvar2; 

The XML content is as follows:

 <parameters> <myvar1>MyValue1</myvar1> <myvar2>MyValue2</myvar2> </parameters> 

C # variables defined:

  foreach (var item in xmlParamInstallation.SelectNodes("parameters")[0].ChildNodes) { ?????? } 

Any idea?

Thanks,

UPDATE 1: the field value in the loop is zero.

 public class ParametersTest { public string myvar1 { get; set; } public string myvar2 {get; set;} } var type = typeof(ParametersTest); foreach (XmlNode item in xmlParamInstallation.SelectNodes("parameters")[0].ChildNodes) { var field = type.GetField(item.LocalName); field.SetValue(field, item.InnerText); } 
+8
reflection c # xml deserialization
source share
5 answers

You can do this using Reflection :

 var type = typeof(SomeClass); var field = type.GetField(item.Name); field.SetValue(null, item.InnerText); 

RE: UPDATE 1

 var parameters = new ParametersTest(); var type = parameters.GetType(); var s = @"<parameters> <MyVar1>MyValue1</MyVar1> <MyVar2>MyValue2</MyVar2> </parameters>"; var xmlParamInstallation = new XmlDocument(); xmlParamInstallation.LoadXml(s); foreach (XmlNode item in xmlParamInstallation.SelectNodes("parameters")[0].ChildNodes) { var field = type.GetProperty(item.LocalName); field.SetValue(parameters, item.InnerText, null); } 
+7
source share

If you want to assign variables based on node names in XML, you have at least a couple of options:

  • Desertification of the XML structure into an object with the corresponding member names
  • Filling Variables with Reflection
  • Fill variables with dynamic method calls / expression trees that know how to read the contents of an XML node in an object property.

All of these approaches involve a more object-oriented approach to the problem, and then just filling in a few variables, but it would be easy to create a lightweight structure with the appropriate members, which is populated by reading an XML document.

You can also use a key-based collection (e.g. Dictionary<string, string> ) to store values ​​if you just want to create a simple collection of names and values ​​from the source XML.

+5
source share

If you put names and values ​​in a dictionary, you can easily get values ​​by name:

 Dictionary<string, string> parameters = xmlParamInstallation.SelectNodes("parameters")[0].ChildNodes .ToDictionary(n => n.Name, n => n.InnerText); myvar1 = parameters["myvar1"]; myvar2 = parameters["myvar2"]; 
+3
source share

You can either do as Default said or look in Reflection. Using the Type.GetMember(string) method, you can find the member with the given name (the tag name in your XML) and set its value.

EDIT
Samich beat me, so I will give him +1 - he also received a sample code.

+2
source share

You can check the XmlSerializer class

+1
source share

All Articles