public void PopulateWithData(string data)
{
var toLoad = data;
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.LoadXml(toLoad);
}
catch
{
// Append new root tags ;)
toLoad = "<root>" + toLoad + "</root>";
xDoc.LoadXml(toLoad);
}
if (!String.IsNullOrEmpty(toLoad))
{
if (xDoc.DocumentElement != null)
{
XmlNodeList children = xDoc.DocumentElement.ChildNodes;
IDictionary<string, int> indexCache = new Dictionary<string, int>();
// spin through each element in the XML
foreach (XmlNode c in children)
{
var hasCorrectIoDirection = true;
if (c.Attributes != null)
{
var columnIoDirectionAttribute = c.Attributes["ColumnIODirection"];
if (columnIoDirectionAttribute != null)
{
var columnIoDirectionValue = columnIoDirectionAttribute.Value;
var hasCorrectIoDirectionFromAttribute = columnIoDirectionValue == enDev2ColumnArgumentDirection.Output.ToString() || columnIoDirectionValue == enDev2ColumnArgumentDirection.Both.ToString();
hasCorrectIoDirection = hasCorrectIoDirectionFromAttribute;
}
}
if (DataListUtil.IsSystemTag(c.Name) && !hasCorrectIoDirection)
{
continue;
}
var recSet = RecordSets.FirstOrDefault(set => set.Name == c.Name);
var shapeRecSet = ShapeRecordSets.FirstOrDefault(set => set.Name == c.Name);
var scalar = Scalars.FirstOrDefault(scalar1 => scalar1.Name == c.Name);
// scalars and recordset fetch
{
if (recSet!=null && shapeRecSet!=null)
{
// fetch recordset index
int fetchIdx;
int idx = indexCache.TryGetValue(c.Name, out fetchIdx) ? fetchIdx : 1; // recset index
// process recordset
var scalars = shapeRecSet.Columns[1];
var colToIoDirection = scalars.ToDictionary(scalar1 => scalar1.Name, scalar1 => scalar1.IODirection);
XmlNodeList nl = c.ChildNodes;
if (!recSet.Columns.ContainsKey(idx))
{
recSet.Columns.Add(idx,new List<IScalar>());
}
else
{
recSet.Columns[idx] = new List<IScalar>();
}
foreach (XmlNode subc in nl)
{
if (colToIoDirection.ContainsKey(subc.Name))
{
var column = new Scalar { Name = subc.Name, Value = subc.InnerText, IODirection = colToIoDirection[subc.Name] };
recSet.Columns[idx].Add(column);
}
}
// update this recordset index
indexCache[c.Name] = ++idx;
}
else
{
if(scalar!=null)
{
scalar.Value = c.InnerXml;
}
}
}
}
}
}
}