Is there a better way to organize this data rather than using string arrays?
c#, xml
Solution
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;
}
Problem
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.