如何反序列化下面给出的代码中的最后一个元素 [英] How Do I Deserialize The Last Element In The Given Below Code

查看:41
本文介绍了如何反序列化下面给出的代码中的最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

私有XElement SerializeDesignerItems(IEnumerable< designeritem> designerItems)

{





XElement serializedItems = new XElement(DesignerItems,



来自designerItems中的项目

let contentXaml = XamlWriter.Save(((DesignerItem)item).Content)

选择新的XElement(DesignerItem,

新XElement(Left,Canvas.GetLeft(item)),

新XElement( Top,Canvas.GetTop(item)),

新XElement(Width,item.Width),

新XElement(Height,item.Height ),

新XElemen t(ID,item.ID),

新XElement(zIndex,Canvas.GetZIndex(item)),

新XElement(IsGroup,项目.IsGroup),

新XElement(ParentID,item.ParentID),

新XElement(Content,contentXaml),

new XElement(Tooltip,value),

new XElement(testValuex,debugClass.commandList.Keys,debugClass.commandList.Values),







);



返回serializedItems;

}

te stValuex是一个由guids和某些命令组成的字典







下面给出的是我的减毒代码







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)+ Offs etY);

Canvas.SetZIndex(item,Int32.Parse(itemXML.Element(zIndex)。Value));

Object content = XamlReader.Load(XmlReader .Create(new StringReader(itemXML.Element(Content)。Value)));





item.Content = content;





返回项目;

}

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;
}

推荐答案

形式上,它可以被称为序列化,但这根本不是真正的序列化。真正的序列化应该是type-不可知,通用。看看人们如何以文明的方式进行这种特殊的序列化。

首先,请阅读: https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx [ ^ ]。



参见:

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
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


这篇关于如何反序列化下面给出的代码中的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆