Convert string to variable name

15,442

Solution 1

You can do it 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);
}

Solution 2

If you are seeking to assign variables based on the names of nodes in XML, you have at least a couple options:

  • Deserialize the XML structure into an object with corresponding member names
  • Populate the variables using reflection
  • Populate the variables using dynamic method calls/expression trees that know how to read the contents of the XML node into an object property.

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

You could also use a key-based collection (like a Dictionary<string, string>) to store the values if you are just looking to build a simple name/value collection from the source XML.

Solution 3

If you put the names and values in a dictionary, you can easily get the 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"];

Solution 4

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

EDIT
Samich beat me, so I'll give him +1 - he's got sample code as well.

Solution 5

You can check out the XmlSerializer class

Share:
15,442
Kris-I
Author by

Kris-I

.NET Consulting

Updated on June 17, 2022

Comments

  • Kris-I
    Kris-I almost 2 years

    I have an XML file, I have a node and I read all ChildNodes. The name of the childNode match to a variable I have to set with the value of this childNode.

    In the loop, I'd like set :

    • myvar1 to MyValue1
    • myvar2 to MyValue2

    The C# Code :

    protected string myvar1;
    protected string myvar2;
    

    The XML content look like this :

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

    C# set variables :

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

    Any idea ?

    Thanks,

    UPDATE 1: the value "field" in the loop is null all the time.

    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);
    }
    
  • Kris-I
    Kris-I over 12 years
    I don't want set variable one by one (if possible)
  • Samich
    Samich over 12 years
    First: Your xml node names doesn't match the properties names including case-sensitivity. Second: you are using properties, so you need to call GetProperty instead of GetField.
  • Guffa
    Guffa over 12 years
    Well, you can do it using reflecction, but I like to give an alternative without reflection. Besides, this way you control which properties are set, not the contents of the XML file.