【发布时间】:2016-10-05 07:49:10
【问题描述】:
我有一个需要分配多个属性的对象列表。
虽然我怀疑数据的来源很重要,但我还是将其包含在背景中。我正在使用 foreach 循环从 XmlDocument 中的选定节点读取属性列表。我创建了一个循环迭代器 i 并尝试根据 attribute.Name 和 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;
这让我发疯了。一定有办法引用attribute.Name的值?
【问题讨论】:
-
您可以使用反射,但听起来您真正需要做的是将 XML 反序列化为 POCO:stackoverflow.com/questions/10518372/…