void Serialize(XNode root, string name, object obj) { if (root == null) { root = _file; } if (obj == null) { return; } if (!SerializeBuiltIn(root, name, obj)) { XNode node = new XNode(root, name); root.Nodes.Add(node); string type = obj.GetType().FullName + "," + obj.GetType().Assembly.GetName().Name; node.AddAttribute("XSerializer.SerializedType", type); if (obj is IXSerializable) { XNode serializableNode = new XNode(node, "IXSerializable"); node.Nodes.Add(serializableNode); (obj as IXSerializable).Serialize(serializableNode); } SerializeProperties(node, name, obj); } }
public static XAttribute SetAttribute(this XNode node, string attrib, string value) { XAttribute att = node.GetAttribute(attrib); if (null != att) { att.Value = value; } else { att = new XAttribute(attrib, value); node.AddAttribute(att); } return(att); }
public static XAttribute GetOrCreateAttribute(this XNode node, string attributeName, string value) { IEnumerable <XAttribute> atts = node.GetAttribute(attributeName, value); XAttribute result = new XAttribute(attributeName, value); if (atts.Count() > 0) { return(atts.First <XAttribute>()); } else { node.AddAttribute(result); return(result); } }
public static bool Serialize(XNode node, object value) { if (value is Enum) { node.AddAttribute("type", "enum"); node.AddAttribute("value", Enum.GetName(value.GetType(), value)); } else if (value is string) { node.AddAttribute("type", "string"); node.AddAttribute("value", (string)value); } else if (value is int) { node.AddAttribute("type", "int"); node.AddAttribute("value", value.ToString()); } else if (value is float) { float val = (float)value; node.AddAttribute("type", "float"); string valueText = null; if (val >= float.MaxValue) { valueText = "max"; } else if (val <= float.MinValue) { valueText = "min"; } else { valueText = string.Format(CultureInfo.InvariantCulture, "{0}", value); } node.AddAttribute("value", valueText); } else if (value is double) { double val = (double)value; node.AddAttribute("type", "double"); string valueText = null; if (val >= double.MaxValue) { valueText = "max"; } else if (val <= double.MinValue) { valueText = "min"; } else { valueText = string.Format(CultureInfo.InvariantCulture, "{0}", value); } node.AddAttribute("value", valueText); } else if (value is long) { node.AddAttribute("type", "long"); node.AddAttribute("value", value.ToString()); } else if (value is bool) { node.AddAttribute("type", "bool"); node.AddAttribute("value", value.ToString()); } else if (value is DateTime) { node.AddAttribute("type", "DateTime"); node.AddAttribute("value", string.Format(CultureInfo.InvariantCulture, "{0}", value)); } else { return(false); } return(true); }