将字符串转换为变量名
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用反射来完成此操作:
回复:更新 1
You can do it using Reflection:
RE: UPDATE 1
如果您想要根据 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:
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.如果将名称和值放入字典中,则可以轻松地按名称获取值:
If you put the names and values in a dictionary, you can easily get the values by name:
您可以按照“默认”所述进行操作,也可以查看反射。通过使用
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.
您可以查看 XmlSerializer 类
You can check out the XmlSerializer class