/// <summary> /// Set value of object's property by XmlNode's attributes /// </summary> /// <param name="obj"></param> /// <param name="?"></param> public static void SetObjectPropertyByAttribute(object obj, XmlNode node) { if (obj == null) { return; } try { PropertyInfo[] pinfo = obj.GetType().GetProperties(); foreach (PropertyInfo p in pinfo) { if (node.Attributes[p.Name] != null) { try { p.SetValue(obj, StringConverterManager.ConvertTo(node.Attributes[p.Name].Value.Trim(), p.PropertyType), null); } catch (Exception) { } } } } catch (Exception) { } }
/// <summary> /// Create single object /// </summary> /// <param name="obj_type"></param> /// <param name="root_node"></param> /// <returns></returns> internal static object DeserializeToSingle(Type obj_type, XmlNode node) { if (node == null) { return(null); } // create a object instance object instance = Activator.CreateInstance(obj_type); PropertyInfo[] pinfos = obj_type.GetProperties(); //only root node has tagName(nodeName),others should be null, //for i have set node with current node. foreach (PropertyInfo p in pinfos) { try { XmlNode n = node.SelectSingleNode(p.Name); if (p.PropertyType.IsArray) // array object { p.SetValue(instance, DeserializeToArray(p.PropertyType, n), null); } else if (StringConverter.CanConvertDirectly(p.PropertyType)) { p.SetValue(instance, StringConverterManager.ConvertTo(n.InnerText, p.PropertyType), null); } else { p.SetValue(instance, DeserializeToSingle(p.PropertyType, n), null); } } catch (Exception) { } } return(instance); }
public static void SetObjectPropertyByElement(object obj, XmlNode node) { if (obj == null) { return; } // create a object instance PropertyInfo[] pinfos = obj.GetType().GetProperties(); foreach (PropertyInfo p in pinfos) { try { XmlNode n = node.SelectSingleNode(p.Name); if (n != null && StringConverter.CanConvertDirectly(p.PropertyType)) { p.SetValue(obj, StringConverterManager.ConvertTo(n.InnerText, p.PropertyType), null); } } catch (Exception) { } } }