/// <summary> /// 反序列化对象 /// </summary> /// <param name="root"></param> public object Deserialize(XElement root) { (root != null).FalseThrow <ArgumentNullException>("root"); XElementFormatter.FormattingStatus = XElementFormattingStatus.Deserializing; try { XmlDeserializeContext context = new XmlDeserializeContext(); context.RootElement = root; context.DeserializeTypeInfo(root); XElement objectProperty = GetObjectElementByID(root, root.AttributeWithAlterName("value", "v", 0)); object result = DeserializeNodeToObject(objectProperty, false, context); context.FillListItems(); return(result); } finally { XElementFormatter.FormattingStatus = XElementFormattingStatus.None; } }
/// <summary> /// 对象序列化为XElement /// </summary> /// <param name="graph"></param> /// <returns></returns> public XElement Serialize(object graph) { XElementFormatter.FormattingStatus = XElementFormattingStatus.Serializing; try { XElement root = XElement.Parse("<Root/>"); XmlSerializeContext context = new XmlSerializeContext() { RootElement = root, ContainerTypeName = graph.GetType().AssemblyQualifiedName }; int objID = context.CurrentID; context.ObjectContext.Add(graph, objID); XElement graphElement = SerializeObjectToNode(root, graph, context); root.SetAttributeValue("v", objID); root.SetAttributeValue("r", true); context.SerializeTypeInfo(root); return(root); } finally { XElementFormatter.FormattingStatus = XElementFormattingStatus.None; } }
/// <summary> /// 反序列化对象 /// </summary> /// <param name="root"></param> public object Deserialize(XElement root) { (root != null).FalseThrow<ArgumentNullException>("root"); XElementFormatter.FormattingStatus = XElementFormattingStatus.Deserializing; try { XmlDeserializeContext context = new XmlDeserializeContext(); context.RootElement = root; context.DeserializeTypeInfo(root); XElement objectProperty = GetObjectElementByID(root, root.AttributeWithAlterName("value", "v", 0)); object result = DeserializeNodeToObject(objectProperty, false, context); context.FillListItems(); return result; } finally { XElementFormatter.FormattingStatus = XElementFormattingStatus.None; } }
/// <summary> /// 对象序列化为XElement /// </summary> /// <param name="graph"></param> /// <returns></returns> public XElement Serialize(object graph) { XElementFormatter.FormattingStatus = XElementFormattingStatus.Serializing; try { XElement root = XElement.Parse("<Root/>"); XmlSerializeContext context = new XmlSerializeContext() { RootElement = root, ContainerTypeName = graph.GetType().AssemblyQualifiedName }; int objID = context.CurrentID; context.ObjectContext.Add(graph, objID); XElement graphElement = SerializeObjectToNode(root, graph, context); root.SetAttributeValue("v", objID); root.SetAttributeValue("r", true); context.SerializeTypeInfo(root); return root; } finally { XElementFormatter.FormattingStatus = XElementFormattingStatus.None; } }