将字符串转换为变量名

发布于 2024-12-24 03:53:24 字数 1010 浏览 1 评论 0原文

我有一个 XML 文件,有一个节点,并且读取了所有子节点。 childNode 的名称与我必须使用该 childNode 的值设置的变量相匹配。

在循环中,我想设置:

  • myvar1 到 MyValue1
  • myvar2 到 MyValue2

C# 代码:

protected string myvar1;
protected string myvar2;

XML 内容如下所示:

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

C# 设置变量:

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

有什么想法吗?

谢谢,

更新 1: 循环中的值“field”始终为空。

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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幻想少年梦 2024-12-31 03:53:24

您可以使用反射来完成此操作:

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

回复:更新 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);
}

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);
}
遮了一弯 2024-12-31 03:53:24

如果您想要根据 XML 中的节点名称分配变量,则至少有几个选项:

  • 将 XML 结构反序列化为具有相应成员名称的对象
  • 使用反射
  • 填充变量 使用动态方法调用/表达式树填充变量知道如何将 XML 节点的内容读入对象属性。

所有这些方法都建议采用一种更加面向对象的方法来解决问题,然后只填充一些变量,但是使用通过读取 XML 文档来填充的适当成员创建轻量级结构会很容易。

如果您只想从源 XML 构建一个简单的名称/值集合,您还可以使用基于键的集合(例如 Dictionary)来存储值。

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.

愚人国度 2024-12-31 03:53:24

如果将名称和值放入字典中,则可以轻松地按名称获取值:

Dictionary<string, string> parameters =
  xmlParamInstallation.SelectNodes("parameters")[0].ChildNodes
  .ToDictionary(n => n.Name, n => n.InnerText);

myvar1 = parameters["myvar1"];
myvar2 = parameters["myvar2"];

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"];
温折酒 2024-12-31 03:53:24

您可以按照“默认”所述进行操作,也可以查看反射。通过使用 Type.GetMember(string) 方法,您可以找到具有给定名称(XML 中的标记名称)的成员并设置其值。

编辑
Samich 打败了我,所以我会给他 +1 - 他也有示例代码。

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.

撩发小公举 2024-12-31 03:53:24

您可以查看 XmlSerializer

You can check out the XmlSerializer class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文