Click here to Skip to main content
15,921,531 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private XElement SerializeDesignerItems(IEnumerable<designeritem> designerItems)
{


XElement serializedItems = new XElement("DesignerItems",

from item in designerItems
let contentXaml = XamlWriter.Save(((DesignerItem)item).Content)
select new XElement("DesignerItem",
new XElement("Left", Canvas.GetLeft(item)),
new XElement("Top", Canvas.GetTop(item)),
new XElement("Width", item.Width),
new XElement("Height", item.Height),
new XElement("ID", item.ID),
new XElement("zIndex", Canvas.GetZIndex(item)),
new XElement("IsGroup", item.IsGroup),
new XElement("ParentID", item.ParentID),
new XElement("Content", contentXaml),
new XElement("Tooltip", value),
new XElement("testValuex", debugClass.commandList.Keys, debugClass.commandList.Values),


)
);

return serializedItems;
}
testValuex is a dictionary consisting of guids and certain commands



the given below is my code for deserilization



private static DesignerItem DeserializeDesignerItem(XElement itemXML, Guid id, double OffsetX, double OffsetY)
{
DesignerItem item = new DesignerItem(id);

item.Width = Double.Parse(itemXML.Element("Width").Value, CultureInfo.InvariantCulture);
item.Height = Double.Parse(itemXML.Element("Height").Value, CultureInfo.InvariantCulture);
item.ParentID = new Guid(itemXML.Element("ParentID").Value);
item.IsGroup = Boolean.Parse(itemXML.Element("IsGroup").Value);
Canvas.SetLeft(item, Double.Parse(itemXML.Element("Left").Value, CultureInfo.InvariantCulture) + OffsetX);
Canvas.SetTop(item, Double.Parse(itemXML.Element("Top").Value, CultureInfo.InvariantCulture) + OffsetY);
Canvas.SetZIndex(item, Int32.Parse(itemXML.Element("zIndex").Value));
Object content = XamlReader.Load(XmlReader.Create(new StringReader(itemXML.Element("Content").Value)));


item.Content = content;


return item;
}
Posted
Updated 10-Aug-15 18:07pm
v2
Comments
Sergey Alexandrovich Kryukov 11-Aug-15 0:15am    
Just forget it...
—SA

1 solution

Formally, it can be called "serialization", but this is not real serialization at all. Real serialization should be type-agnostic, universal. Instead of doing such ad-hoc serialization, look at how people do it in civilized way.
First of all, read this: https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

See also:
https://en.wikipedia.org/wiki/Serialization[^],
https://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.runtime.serialization%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900