简体   繁体   English

有没有更好的方法来组织这些数据而不是使用字符串数组?

[英]Is there a better way to organize this data rather than using string arrays?

I have two loops that pull data from two XML sources: 我有两个循环从两个XML源提取数据:

Loop1: 循环1:

foreach (XmlNode nodes in node.ChildNodes)
            {
                if (nodes.Name == "DEFAULT")
                    defaults[count] = nodes.InnerText;
                if (nodes.Name == "TYPE"
                    types[count] = nodes.InnerText;
                if (nodes.Name == "COL_NAM"
                    names[count] = nodes.InnerText;

            }
            count++;

Loop2: 循环2:

foreach (XmlNode nodes in root.ChildNodes)
        {
            vals[i] = nodes.InnerText;
            cols[i] = nodes.Name;
            i++;
        }

Somehow, I want to organize this data into one ultimate object. 不知何故,我想将这些数据组织成一个终极对象。 The object needs to have 4 parts: Names, Types, Values, and Defaults. 该对象需要有4个部分:名称,类型,值和默认值。 Essentially, I want to group together everything from Loop1, and then everything from Loop2, and then add together the two objects into one object matching names from Loop1 with cols from Loop2. 本质上,我想将Loop1中的所有内容组合在一起,然后将Loop2中的所有内容组合在一起,然后将两个对象组合成一个对象,该对象与Loop1中的名称和来自Loop2的cols匹配。 Ideally, the amount of nodes in Loop2 could be less than that of Loop1. 理想情况下,Loop2中的节点数量可能小于Loop1的节点数量。 But, if that's not possible I can work around it. 但是,如果那是不可能的,我可以解决它。

For a better picture of the final object: 为了更好地了解最终对象:

object everything = {{names}, {types}, {values}, {defaults}};

Names will come from BOTH loops, and will be the 'Key' to the object. 名称将来自BOTH循环,并将成为对象的“键”。 Types and defaults will come from Loop1, and values will come from Loop2. 类型和默认值将来自Loop1,值将来自Loop2。 The concatenation will match using Name/Col. 串联将使用Name / Col匹配。

PS: I tried to do this using 2D String arrays, but ran into trouble when trying to combine the two matching the cols and names fields. PS:我试图使用2D String数组来做这件事,但在尝试将两个匹配的cols和names字段组合时遇到了麻烦。

You could use the Dictionary class and use the Name as the key for your object. 您可以使用Dictionary类并使用Name作为对象的键。

For example: 例如:

public class MyObj{
  public string Name{get;set;};
  public string Type{get;set;};
  public string Value{get;set;};
  public string Default{get;set;};
}

And modify your loop to use it: 并修改你的循环使用它:

var allObjs = new Dictionary<string, MyObj>();   


foreach (XmlNode nodes in node.ChildNodes)
{
  var obj = new MyObj();
    if (nodes.Name == "DEFAULT")
      obj.Default = nodes.InnerText;
    ...
    allObjs.Add(obj.Name, obj);
}

Then on your second loop retrieve your existing object using the key in order to update the value. 然后在第二个循环中使用键检索现有对象以更新值。 Something like this: 像这样的东西:

foreach (XmlNode nodes in root.ChildNodes)
{
  var myObj = allObs[nodes.Name];
  myObj.Value = nodes.InnerText;
}

Well, you can use XPath to select your values. 好吧,您可以使用XPath来选择您的值。

public class Ultimate
{
    public Ultimate(XmlNode node)
    {
       XmlNode child = node.SelectSingleObject("./COL_NAM");
       if (child == null) throw new InvalidArgument("COL_NAM not found");
       Name = child.InnerText;

       XmlNode child = node.SelectSingleObject("./TYPE");
       if (child == null) throw new InvalidArgument("TYPE not found");
       Type = child.InnerText;

       XmlNode child = node.SelectSingleObject("./DEFAULT");
       if (child == null) throw new InvalidArgument("DEFAULT not found");
       Default = child.InnerText;

       XmlNode child = node.SelectSingleObject("./COL_NAM");
       if (child == null) throw new InvalidArgument("COL_NAM not found");
       Name = child.InnerText;

    }
    public string Name;
    public string Type;
    public string Value;
    public string Default;
}

// Reading objects would be like this: //读取对象将是这样的:

Dictionary<string, Ultimate> ultimateCollection = new Dictionary<string, Ultimate>();
foreach(XmlNode node in xmlDocument1.SelectNodes("/DATA1"))
{
    Ultimate ultimate = new Ultimate(node);
    ultimateCollection.Add(ultimate.Name, ultimate);
}

foreach(XmlNode node in root.ChildNodes)
{
    Ultimate ultimate;
    if (ultimateCollection.TryGetValue(node.Name, out ultimate))
        ultimate.Values = node.InnerText;
}

I hope this will help you in your quest. 我希望这能帮助你完成任务。

From what is sounds like your describing I would use XElement \\ LINQ. 从你的描述听起来我会使用XElement \\ LINQ。 http://codingsense.wordpress.com/2008/09/23/join-xml-using-linq/ http://codingsense.wordpress.com/2008/09/23/join-xml-using-linq/

..and/or.. ..和/或..

Serialize/deserialze the xml to C# objects http://www.java2s.com/Code/CSharp/XML/XmlSerializationHelper.htm 将xml序列化/反序列化为C#对象http://www.java2s.com/Code/CSharp/XML/XmlSerializationHelper.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM