internal static Dictionary <string, object> DeserializeXml(string xmlStr, Type type) { Dictionary <string, object> result = new Dictionary <string, object>(); XmlDocument contentXmlDoc = new XmlDocument(); contentXmlDoc.LoadXml(xmlStr); XmlNodeList nodeList = contentXmlDoc.ChildNodes; for (int i = 0; i < nodeList.Count; i++) { XmlNode root = nodeList.Item(i); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo p in properties) { Type propertyType = p.PropertyType; NameInMapAttribute attribute = p.GetCustomAttribute(typeof(NameInMapAttribute)) as NameInMapAttribute; string realName = attribute == null ? p.Name : attribute.Name; if (root.Name == realName) { if (!typeof(TeaModel).IsAssignableFrom(propertyType)) { result.Add(realName, root.InnerText); } else { result.Add(realName, getDictFromXml(root, propertyType)); } } } } return(result); }
private static Dictionary <string, object> getDictFromXml(XmlNode element, Type type) { Dictionary <string, object> nodeDict = new Dictionary <string, object>(); PropertyInfo[] properties = type.GetProperties(); for (int i = 0; i < properties.Length; i++) { PropertyInfo p = properties[i]; Type propertyType = p.PropertyType; NameInMapAttribute attribute = p.GetCustomAttribute(typeof(NameInMapAttribute)) as NameInMapAttribute; string realName = attribute == null ? p.Name : attribute.Name; XmlNodeList node = element.SelectNodes(realName); if (node != null && node.Count > 0 && StringUtils.SubStringCount(node[0].OuterXml, realName) > 1) { if (typeof(IList).IsAssignableFrom(propertyType)) { Type innerPropertyType = propertyType.GetGenericArguments() [0]; if (typeof(TeaModel).IsAssignableFrom(innerPropertyType)) { IList dicList = new List <Dictionary <string, object> >(); for (int j = 0; j < node.Count; j++) { dicList.Add(getDictFromXml(node.Item(j), innerPropertyType)); } nodeDict.Add(realName, dicList); } else { var dicList = (IList)Activator.CreateInstance(propertyType); for (int j = 0; j < node.Count; j++) { var value = mapObj(innerPropertyType, node.Item(j).InnerText); dicList.Add(value); } nodeDict.Add(realName, dicList); } } else if (typeof(TeaModel).IsAssignableFrom(propertyType)) { nodeDict.Add(realName, getDictFromXml(node.Item(0), propertyType)); } else { string value = node.Item(0).InnerText; nodeDict.Add(realName, mapObj(propertyType, value)); } } else { nodeDict.Add(realName, null); } } return(nodeDict); }
private static void GetXml(TeaModel model, XElement element) { Type type = model.GetType(); PropertyInfo[] properties = type.GetProperties(); for (int i = 0; i < properties.Length; i++) { PropertyInfo propertyInfo = properties[i]; Type property = propertyInfo.PropertyType; NameInMapAttribute attribute = propertyInfo.GetCustomAttribute(typeof(NameInMapAttribute)) as NameInMapAttribute; string realName = attribute == null ? propertyInfo.Name : attribute.Name; XElement node = new XElement(realName); GetXmlFactory(propertyInfo.GetValue(model), node, element); } }
internal static string SerializeXmlByModel(TeaModel obj) { Type type = obj.GetType(); PropertyInfo[] properties = type.GetProperties(); if (obj == null || properties.Length == 0) { return(string.Empty); } PropertyInfo propertyInfo = properties[0]; NameInMapAttribute attribute = propertyInfo.GetCustomAttribute(typeof(NameInMapAttribute)) as NameInMapAttribute; string realName = attribute == null ? propertyInfo.Name : attribute.Name; object rootObj = propertyInfo.GetValue(obj); XElement element = new XElement(realName); GetXmlFactory(rootObj, element); return(element.ToString()); }