Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic lightweight diagram XML (Read 1987 times)
asafs
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 2
Joined: Sep 17th, 2009
lightweight diagram XML
Sep 17th, 2009 at 4:41am
Print Post  
Hi,

I'm trying to save a lighter xml of the diagram with only the critical attributes . for instance I want to save for a link only it's origin, destination and location. all other features are set when the diagram is loaded and never changed. I want to persist the diagram XML to a database and keep it small and light.

Is there a way to do that?

Thanks,

Asaf
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: lightweight diagram XML
Reply #1 - Sep 17th, 2009 at 2:03pm
Print Post  
Try this:

Code
Select All
XDocument document = new XDocument();
SaveLightXml(diagram, document);
diagram.ClearAll();
LoadLightXml(diagram, document);

private void SaveLightXml(Diagram diagram, XDocument document)
{
	XmlPersistContext context = new XmlPersistContext(diagram, document);
	XElement root = new XElement("Diagram");
	document.Add(root);

	XElement nodesEl = context.AddChildElement("Nodes", root);
	foreach (DiagramNode node in diagram.Nodes)
	{
		ShapeNode shapeNode = node as ShapeNode;
		XElement nodeEl = context.AddChildElement("Node", nodesEl);
		nodeEl.SetAttributeValue("Id", shapeNode.ZIndex);
		context.WriteRectangleF(shapeNode.Bounds, "Bounds", nodeEl);
		context.WriteString(shapeNode.Text, "Text", nodeEl);
	}

	XElement linksEl = context.AddChildElement("Links", root);
	foreach (DiagramLink link in diagram.Links)
	{
		XElement linkEl = context.AddChildElement("Link", linksEl);
		context.WriteInt(link.Destination.ZIndex, "Destination", linkEl);
		context.WriteInt(link.Origin.ZIndex, "Origin", linkEl);
	}
}

private void LoadLightXml(Diagram diagram, XDocument document)
{
	XmlPersistContext context = new XmlPersistContext(diagram, document);
	XElement root = document.Root;

	Dictionary<int, ShapeNode> dictionary = new Dictionary<int, ShapeNode>();

	XElement nodesEl = root.Element("Nodes");
	var nodeElements = nodesEl.Elements("Node");
	foreach (XElement nodeEl in nodeElements)
	{
		ShapeNode node = diagram.Factory.CreateShapeNode(10, 10, 10, 10);
		int id = int.Parse(nodeEl.Attribute("Id").Value);
		node.Bounds = context.ReadRectangleF("Bounds", nodeEl);
		node.Text = context.ReadString("Text", nodeEl);

		dictionary.Add(id, node);
	}

	XElement linksEl = root.Element("Links");
	var linkElements = linksEl.Elements("Link");
	foreach (XElement linkEl in linkElements)
	{
		int dest = context.ReadInt("Destination", linkEl);
		int orig = context.ReadInt("Origin", linkEl);
		ShapeNode destNode = dictionary[dest];
		ShapeNode origNode = dictionary[orig];
		diagram.Factory.CreateDiagramLink(origNode, destNode);
	}
}
 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
asafs
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 2
Joined: Sep 17th, 2009
Re: lightweight diagram XML
Reply #2 - Sep 17th, 2009 at 5:58pm
Print Post  
Wow!

Thanks for the quick and helpful reply, I'll try your code.
Thanks alot Stoyan !!!
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint