private static void DeSerialize(this IXSettings obj, XElement xElement) { foreach (var propertyInfo in obj.GetType().GetProperties()) { if (propertyInfo.GetCustomAttribute(typeof(XmlIgnoreAttribute)) == null && propertyInfo.CanWrite && propertyInfo.CanRead) { try { string elementName = propertyInfo.Name; XmlElementAttribute attribute = (XmlElementAttribute)propertyInfo.GetCustomAttribute(typeof(XmlElementAttribute)); if (attribute != null) { elementName = attribute.ElementName; } XElement element = xElement.Element(elementName); Type contentType = MyTypeExtension.GetAssemblyQualifiedType(element.Attribute("type").Value); object o = ToObject(element, contentType); propertyInfo.SetValue(obj, o); } catch (Exception) { } } } }
public static void Save(this IXSettings obj) { XElement xElement = obj.Serialize(); XDocument xDocument = new XDocument(new XComment(obj._Comment), xElement); if (!Directory.Exists(Path.GetDirectoryName(obj._FileName("xml")))) { Directory.CreateDirectory(Path.GetDirectoryName(obj._FileName("xml"))); } xDocument.Save(obj._FileName("xml")); }
private static XElement Serialize(this IXSettings obj) { XElement xElement = new XElement(obj.GetType().ToString()); //XDocument xDocument = CreateXml(this.GetType().ToString()); foreach (var propertyInfo in obj.GetType().GetProperties()) { XElement element = ToXElement(new KeyValuePair <PropertyInfo, object>(propertyInfo, obj)); if (element != null) { xElement.Add(element); } } return(xElement); }
public static void Load(this IXSettings obj) { if (File.Exists(obj._FileName("xml"))) { XDocument xDocument; try { xDocument = XDocument.Load(obj._FileName("xml")); } catch (Exception)//>>说明文件被破坏 { File.Delete(obj._FileName("xml")); return; } obj.DeSerialize(xDocument.Root); } obj.OnSettingsInitialized(); }