예제 #1
0
        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);
        }
예제 #2
0
        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)
                    {
                    }
                }
            }
        }