简体   繁体   中英

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:

Loop1:

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:

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. 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. Ideally, the amount of nodes in Loop2 could be less than that of 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. Types and defaults will come from Loop1, and values will come from Loop2. The concatenation will match using 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.

You could use the Dictionary class and use the Name as the key for your object.

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.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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