private void DeserializeNodeToCollection(IEnumerable <XElement> valueNodes, XmlDeserializeContext context, Action <int, object> action) { int i = 0; foreach (XElement item in valueNodes) { object itemData = null; string elementID = item.Attribute("v").Value; if (item.AttributeWithAlterName("isRef", "r", false) == true) { XElement objectElement = GetObjectElementByID(context.RootElement, item.AttributeWithAlterName("value", "v", 0)); itemData = DeserializeNodeToObject(objectElement, false, context); } else { Type type = context.GetTypeInfo(item.AttributeWithAlterName("ownerTypeID", "oti", string.Empty)); itemData = item.AttributeWithAlterName("value", "v", TypeCreator.GetTypeDefaultValue(type)); itemData = DataConverter.ChangeType(itemData, type); } action(i, itemData); i++; } }
/// <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; } }
public void Deserialize(XElement node, XmlDeserializeContext context) { this.Name = node.Attribute("_na", this.Name); this.DisplayName = node.Attribute("_dn", this.DisplayName); this.Columns = node.Attribute("_columns", this.Columns); this.DefaultRowHeight = node.Attribute("_drh", this.DefaultRowHeight); }
private static XElement GetObjectElementByID(XmlDeserializeContext context, int id) { XElement element = null; context.ObjectElements.TryGetValue(id, out element); return(element); }
public void PropertyValueSerializeTest() { PropertyValue pv = PreparePropertyValue(); XElementFormatter formatter = new XElementFormatter(); XmlSerializeContext context = new XmlSerializeContext(); XElement root = new XElement("root"); pv.Serialize(root, context); Console.WriteLine(root.ToString()); XmlDeserializeContext dcontext = new XmlDeserializeContext(); PropertyValue newPropertyValue = new PropertyValue(new PropertyDefine()); newPropertyValue.Deserialize(root, dcontext); //Assert.AreEqual(root.ToString(), rootReserialized.ToString()); Assert.AreEqual(pv.StringValue, newPropertyValue.StringValue); Assert.AreEqual(pv.Definition.Name, newPropertyValue.Definition.Name); }
public void Deserialize(XElement node, XmlDeserializeContext context) { object pv = null; int objID = node.Attribute("v", -1); if (context.ObjectContext.TryGetValue(objID, out pv) == true) { this._LayoutSection = ((PropertyLayout)pv).Clone().LayoutSection; } else { this._LayoutSection = new PropertyLayoutSectionDefine(); this._LayoutSection.Deserialize(node, context); objID = node.Attribute("id", -1); if (objID > -1) { context.ObjectContext.Add(objID, this); } } }
private void DeserializeXmlSerilizableList(XElement parent, object graph, XmlDeserializeContext context) { if (graph is IXmlSerilizableList) { var valueNodes = from vNodes in parent.Descendants("Items").Descendants("I") select vNodes; if (valueNodes.FirstOrDefault() == null) { valueNodes = from vNodes in parent.Descendants("Items").Descendants("Item") select vNodes; } IXmlSerilizableList list = (IXmlSerilizableList)graph; list.Clear(); //DeserializeNodeToCollection(valueNodes, context, (i, itemData) => list.Add(itemData)); DeserializeNodeToCollection(valueNodes, context, (i, itemData) => { context.ListItems.Add(new XmlListDeserializeItem(list, itemData)); }); } }
private void DeserializeNodesToProperties(XElement parent, object graph, XmlDeserializeContext context) { TypeFields tf = TypeFields.GetTypeFields(graph.GetType()); foreach (KeyValuePair <TypeFieldInfo, ExtendedFieldInfo> kp in tf.Fields) { ExtendedFieldInfo efi = kp.Value; if (efi.IsNotSerialized == false && OnFieldCanXElementSerialize(efi)) { System.Type realType = efi.FieldInfo.FieldType; var propertiesElement = from property in parent.Descendants("F") where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName || property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) && context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType select property; if (propertiesElement.FirstOrDefault() == null) { propertiesElement = from property in parent.Descendants("Field") where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName || property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) && context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType select property; } XElement propertyElement = propertiesElement.FirstOrDefault(); if (propertyElement != null) { object data = null; if (propertyElement.AttributeWithAlterName("isRef", "r", false) == true) { XElement objectElement = GetObjectElementByID(context.RootElement, propertyElement.AttributeWithAlterName("value", "v", 0)); if (objectElement != null) { bool ignoreError = propertyElement.Attribute("ide", false); if (ignoreError == false) { XElementFieldSerializeAttribute attr = AttributeHelper.GetCustomAttribute <XElementFieldSerializeAttribute>(efi.FieldInfo); if (attr != null) { ignoreError = attr.IgnoreDeserializeError; } } data = DeserializeNodeToObject(objectElement, ignoreError, context); SetValueToObject(efi.FieldInfo, graph, data); } } else { data = propertyElement.AttributeWithAlterName("value", "v", TypeCreator.GetTypeDefaultValue(realType)); if (Convertible(realType, data)) { SetValueToObject(efi.FieldInfo, graph, ConvertData(efi.FieldInfo, data)); } } } } } DeserializeXmlSerilizableList(parent, graph, context); }
private object DeserializeNodeToObject(XElement objectNode, bool ignoreError, XmlDeserializeContext context) { object data = null; int objID = objectNode.Attribute("id", 0); if (context.ObjectContext.TryGetValue(objID, out data) == false) { try { Type type = context.GetTypeInfo(objectNode.AttributeValueWithAlterName("ownerTypeID", "oti")); XElementFieldSerializeAttribute attr = AttributeHelper.GetCustomAttribute <XElementFieldSerializeAttribute>(type); if (attr != null && attr.IgnoreDeserializeError == true) { ignoreError = attr.IgnoreDeserializeError; } data = CreateSerializableInstance(type); context.ObjectContext.Add(objID, data); if ((data is IXElementSerializable) && objectNode.Attribute("cs", false) == true) { ((IXElementSerializable)data).Deserialize(objectNode, context); } else { string objectValue = objectNode.Element("Value", string.Empty); if (objectValue.IsNotEmpty()) { SerializationBinder binder = this.Binder ?? UnknownTypeStrategyBinder.Instance; data = SerializationHelper.DeserializeStringToObject(objectValue, SerializationFormatterType.Binary, binder); } else { DeserializeNodesToProperties(objectNode, data, context); } } if (data is IXmlDeserialize) { ((IXmlDeserialize)data).AfterDeserialize(context); } } catch (System.Exception) { if (ignoreError == false) { throw; } } } return(data); }
/// <summary> /// 为通用的序列化而显示实现接口IXElementSerializable /// </summary> /// <param name="node"></param> /// <param name="context"></param> void IXElementSerializable.Deserialize(XElement node, XmlDeserializeContext context) { this._Definition = new PropertyDefine(); this._Definition.Deserialize(node, context); string sv = node.Attribute("_sv", this._StringValue); if (string.IsNullOrEmpty(sv)) this._StringValue = this._Definition.DefaultValue; else this._StringValue = sv; }
public void Deserialize(XElement node, XmlDeserializeContext context) { object pv = null; int objID = node.Attribute("v", -1); if (context.ObjectContext.TryGetValue(objID, out pv) == true) { this._Definition = ((PropertyValue)pv).Clone().Definition; } else { this._Definition = new PropertyDefine(); this._Definition.Deserialize(node, context); objID = node.Attribute("id", -1); if (objID > -1) { context.ObjectContext.Add(objID, this); } } string sv = node.Attribute("_sv", this._StringValue); if (string.IsNullOrEmpty(sv)) this._StringValue = this._Definition.DefaultValue; else this._StringValue = sv; }
private void DeserializeNodeToCollection(IEnumerable<XElement> valueNodes, XmlDeserializeContext context, Action<int, object> action) { int i = 0; foreach (XElement item in valueNodes) { object itemData = null; string elementID = item.Attribute("v").Value; if (item.AttributeWithAlterName("isRef", "r", false) == true) { XElement objectElement = GetObjectElementByID(context.RootElement, item.AttributeWithAlterName("value", "v", 0)); itemData = DeserializeNodeToObject(objectElement, false, context); } else { Type type = context.GetTypeInfo(item.AttributeWithAlterName("ownerTypeID", "oti", string.Empty)); itemData = item.AttributeWithAlterName("value", "v", TypeCreator.GetTypeDefaultValue(type)); itemData = DataConverter.ChangeType(itemData, type); } action(i, itemData); i++; } }
private void DeserializeXmlSerilizableList(XElement parent, object graph, XmlDeserializeContext context) { if (graph is IXmlSerilizableList) { var valueNodes = from vNodes in parent.Descendants("Items").Descendants("I") select vNodes; if (valueNodes.FirstOrDefault() == null) valueNodes = from vNodes in parent.Descendants("Items").Descendants("Item") select vNodes; IXmlSerilizableList list = (IXmlSerilizableList)graph; list.Clear(); //DeserializeNodeToCollection(valueNodes, context, (i, itemData) => list.Add(itemData)); DeserializeNodeToCollection(valueNodes, context, (i, itemData) => { context.ListItems.Add(new XmlListDeserializeItem(list, itemData)); }); } }
private void DeserializeNodesToProperties(XElement parent, object graph, XmlDeserializeContext context) { TypeFields tf = TypeFields.GetTypeFields(graph.GetType()); foreach (KeyValuePair<TypeFieldInfo, ExtendedFieldInfo> kp in tf.Fields) { ExtendedFieldInfo efi = kp.Value; if (efi.IsNotSerialized == false && OnFieldCanXElementSerialize(efi)) { System.Type realType = efi.FieldInfo.FieldType; var propertiesElement = from property in parent.Descendants("F") where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName || property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) && context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType select property; if (propertiesElement.FirstOrDefault() == null) propertiesElement = from property in parent.Descendants("Field") where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName || property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) && context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType select property; XElement propertyElement = propertiesElement.FirstOrDefault(); if (propertyElement != null) { object data = null; if (propertyElement.AttributeWithAlterName("isRef", "r", false) == true) { XElement objectElement = GetObjectElementByID(context.RootElement, propertyElement.AttributeWithAlterName("value", "v", 0)); if (objectElement != null) { bool ignoreError = propertyElement.Attribute("ide", false); if (ignoreError == false) { XElementFieldSerializeAttribute attr = AttributeHelper.GetCustomAttribute<XElementFieldSerializeAttribute>(efi.FieldInfo); if (attr != null) ignoreError = attr.IgnoreDeserializeError; } data = DeserializeNodeToObject(objectElement, ignoreError, context); SetValueToObject(efi.FieldInfo, graph, data); } } else { data = propertyElement.AttributeWithAlterName("value", "v", TypeCreator.GetTypeDefaultValue(realType)); if (Convertible(realType, data)) SetValueToObject(efi.FieldInfo, graph, ConvertData(efi.FieldInfo, data)); } } } } DeserializeXmlSerilizableList(parent, graph, context); }
private object DeserializeNodeToObject(XElement objectNode, bool ignoreError, XmlDeserializeContext context) { object data = null; int objID = objectNode.Attribute("id", 0); if (context.ObjectContext.TryGetValue(objID, out data) == false) { try { Type type = context.GetTypeInfo(objectNode.AttributeValueWithAlterName("ownerTypeID", "oti")); XElementFieldSerializeAttribute attr = AttributeHelper.GetCustomAttribute<XElementFieldSerializeAttribute>(type); if (attr != null && attr.IgnoreDeserializeError == true) ignoreError = attr.IgnoreDeserializeError; data = CreateSerializableInstance(type); context.ObjectContext.Add(objID, data); if ((data is IXElementSerializable) && objectNode.Attribute("cs", false) == true) { ((IXElementSerializable)data).Deserialize(objectNode, context); } else { string objectValue = objectNode.Element("Value", string.Empty); if (objectValue.IsNotEmpty()) { SerializationBinder binder = this.Binder ?? UnknownTypeStrategyBinder.Instance; data = SerializationHelper.DeserializeStringToObject(objectValue, SerializationFormatterType.Binary, binder); } else { DeserializeNodesToProperties(objectNode, data, context); } } if (data is IXmlDeserialize) ((IXmlDeserialize)data).AfterDeserialize(context); } catch (System.Exception) { if (ignoreError == false) throw; } } return data; }
/// <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; } }