public int Compare(PropertyInfo x, PropertyInfo y) { XmlMappingAttribute attrX = x.GetCustomAttributes(typeof(XmlMappingAttribute), true)[0] as XmlMappingAttribute; XmlMappingAttribute attrY = y.GetCustomAttributes(typeof(XmlMappingAttribute), true)[0] as XmlMappingAttribute; return(attrX.Index.CompareTo(attrY.Index)); }
public virtual void BuildXmlNode(XmlDocument doc, ref XmlNode refNode, string subName) { foreach (PropertyInfo property in GetPropertyInfoList()) { object[] attrs = property.GetCustomAttributes(typeof(XmlMappingAttribute), true); if (attrs.Length > 0) { XmlMappingAttribute attr = attrs[0] as XmlMappingAttribute; if (property.PropertyType.IsSubclassOf(typeof(XmlMappingObject))) { XmlMappingObject obj = property.GetValue(this, null) as XmlMappingObject; if (attr.ObjectType == XmlObjectType.List) { obj.BuildXmlNode(doc, ref refNode, attr.MappingName); } else { XmlNode node = CreateElement(doc, attr.MappingName, ""); obj.BuildXmlNode(doc, ref node, attr.MappingName); refNode.AppendChild(node); } } else if (attr.ObjectType == XmlObjectType.List) { List <string> list = property.GetValue(this, null) as List <string>; foreach (string item in list) { XmlNode node = CreateElement(doc, attr.MappingName, item); refNode.AppendChild(node); } } else if (property.PropertyType.IsEnum) { int value = (int)property.GetValue(this, null); if (attr.MappingType == MappingType.Attribute) { refNode.Attributes.Append(CreateAttribute(doc, attr.MappingName, value)); } else { refNode.AppendChild(CreateElement(doc, attr.MappingName, value)); } } else { object value = property.GetValue(this, null); if (attr.MappingType == MappingType.Attribute) { refNode.Attributes.Append(CreateAttribute(doc, attr.MappingName, value)); } else { refNode.AppendChild(CreateElement(doc, attr.MappingName, value)); } } } } }
public virtual void AnalyzeXmlNode(XmlNode xmlNode) { if (xmlNode == null) { return; } foreach (PropertyInfo property in GetPropertyInfoList()) { object[] attrs = property.GetCustomAttributes(typeof(XmlMappingAttribute), true); if (attrs.Length > 0) { XmlMappingAttribute attr = attrs[0] as XmlMappingAttribute; if (property.PropertyType.IsSubclassOf(typeof(XmlMappingObject))) { XmlMappingObject child = property.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(null) as XmlMappingObject; XmlNode node; if (attr.ObjectType == XmlObjectType.List) { node = GetNodeList(xmlNode, attr.MappingName); } else { node = GetChildNode(xmlNode, attr.MappingName); } child.AnalyzeXmlNode(node); property.SetValue(this, child, null); } else if (attr.ObjectType == XmlObjectType.List) { XmlNode node = GetNodeList(xmlNode, attr.MappingName); List <string> list = new List <string>(); foreach (XmlNode child in node.ChildNodes) { list.Add(GetNodeValue(child, attr.MappingType)); } property.SetValue(this, list, null); } else if (property.PropertyType.IsEnum) { object enumObj = Enum.Parse(property.PropertyType, GetNodeValue(xmlNode, attr.MappingName, attr.MappingType)); property.SetValue(this, enumObj, null); } else if (property.PropertyType.Equals(typeof(string))) { property.SetValue(this, GetNodeValue(xmlNode, attr.MappingName, attr.MappingType), null); } else { string value = GetNodeValue(xmlNode, attr.MappingName, attr.MappingType); Type type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; property.SetValue(this, Convert.ChangeType(value, type), null); } } } }