private DeserializeNetwork ( string filePath ) : void | ||
filePath | string | |
return | void |
internal void DeserializeNetwork(string filePath)
{
var tempNodeCollection = new List<Node>();
NewFile();
// Create an reader
using (var reader = new XmlTextReader(filePath))
{
reader.MoveToContent();
var enumString = reader.GetAttribute("GraphFlowDirection");
if (enumString != null)
{
ImportFlowDirection =
(GraphFlowDirections) Enum.Parse(typeof (GraphFlowDirections), enumString, true);
}
reader.ReadToDescendant("Nodes");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.Name != null)
{
var type = Type.GetType(reader.Name);
if (type == null)
{
try // try to find type in entry assembly
{
var assembly = Assembly.GetEntryAssembly();
type = assembly.GetTypes().FirstOrDefault(t => t.FullName == reader.Name);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
try // try to find type in ExternalNodeTypes
{
type = ExternalNodeTypes.ToArray()
.FirstOrDefault(t => t.FullName == reader.Name);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
if (type != null)
{
if (type.IsSubclassOf(typeof (Node)))
{
var node = (Node) Activator.CreateInstance(type, this);
node.DeserializeNetwork(reader);
tempNodeCollection.Add(node);
}
else if (type == typeof (Connector))
{
Node startNode = null;
Node endNode = null;
var value = reader.GetAttribute("StartNode");
if (value != null)
{
startNode =
NodeCollection.FirstOrDefault(node => node.Guid == new Guid(value));
}
value = reader.GetAttribute("EndNode");
if (value != null)
endNode = NodeCollection.FirstOrDefault(node => node.Guid == new Guid(value));
value = reader.GetAttribute("StartIndex");
var startIndex = Convert.ToInt32(value);
value = reader.GetAttribute("EndIndex");
var endIndex = Convert.ToInt32(value);
if (startNode != null && endNode != null)
{
var startPort = startNode.OutputPorts[startIndex];
var endPort = endNode.InputPorts[endIndex];
if (startPort != null && endPort != null)
{
var connector = new Connector(this, startPort, endPort);
ConnectorCollection.Add(connector);
}
}
}
}
}
break;
case XmlNodeType.Text:
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
break;
case XmlNodeType.Comment:
break;
case XmlNodeType.EndElement:
break;
}
}
}
foreach (var node in tempNodeCollection)
{
node.Show();
}
}
}