public void LoadFromXml(XmlElement xml) { this.LoadPropertiesCore(xml); Table = NameWithSchema.LoadFromXml(xml.FindElement("Table")); }
public static void LoadPropertiesCore(this object o, XmlElement xml) { if (xml == null) { return; } foreach (PropertyInfo prop in o.GetType().GetProperties().OrderBy(x => x.Name)) { bool propHandled = false; //MethodInfo setMethod = prop.GetSetMethod(); //if (setMethod == null) continue; // not SET method available string sval = null; foreach (XmlAttribAttribute attr in prop.GetCustomAttributes(typeof(XmlAttribAttribute), true)) { if (xml.HasAttribute(attr.Name ?? prop.Name)) { sval = xml.GetAttribute(attr.Name ?? prop.Name); } } foreach (XmlElemAttribute attr in prop.GetCustomAttributes(typeof(XmlElemAttribute), true)) { if (prop.PropertyType == typeof(NameWithSchema)) { propHandled = true; var elem = xml.SelectSingleNode(attr.Name ?? prop.Name) as XmlElement; if (elem != null) { prop.CallSet(o, NameWithSchema.LoadFromXml(elem)); } } else if (prop.PropertyType == typeof(DateTimeEx)) { propHandled = true; var elem = xml.SelectSingleNode(attr.Name ?? prop.Name) as XmlElement; if (elem != null && !String.IsNullOrEmpty(elem.InnerText)) { prop.CallSet(o, DateTimeEx.ParseNormalized(elem.InnerText)); } } else if (prop.PropertyType == typeof(Dictionary <string, string>)) { propHandled = true; var elem = xml.SelectSingleNode(attr.Name ?? prop.Name) as XmlElement; if (elem != null) { var dct = new Dictionary <string, string>(); foreach (XmlElement itemXml in elem) { dct[itemXml.GetAttribute("Key")] = itemXml.InnerText; } prop.CallSet(o, dct); } } else { if (xml.SelectSingleNode(attr.Name ?? prop.Name) != null) { sval = xml.SelectSingleNode(attr.Name ?? prop.Name).InnerText; } if (attr.Encrypt) { sval = SafeDecodeString(sval); } } } foreach (XmlSubElemAttribute attr in prop.GetCustomAttributes(typeof(XmlSubElemAttribute), true)) { object subval = prop.CallGet(o); var elem = xml.FindElement(attr.Name ?? prop.Name); if (elem != null) { if (subval == null) { try { subval = prop.PropertyType.CreateNewChildInstance(o); prop.CallSet(o, subval); } catch { subval = null; } } if (subval != null) { LoadProperties(subval, elem); } } } #if !NETSTANDARD2_0 foreach (SettingAttribute attr in prop.GetCustomAttributes(typeof(SettingAttribute), true)) { var elem = xml.SelectSingleNode(prop.Name); if (elem != null) { sval = elem.InnerText; } } #endif foreach (XmlThisAttribute attr in prop.GetCustomAttributes(typeof(XmlThisAttribute), true)) { object subval = prop.CallGet(o); if (subval == null) { try { subval = prop.PropertyType.CreateNewInstance(); prop.CallSet(o, subval); } catch { subval = null; } } if (subval != null) { LoadProperties(subval, xml); } } foreach (XmlCollectionAttribute attr in prop.GetCustomAttributes(typeof(XmlCollectionAttribute), true)) { string name = attr.GetElemName(prop); var lst = xml.GetElementsByTagName(name); if (lst.Count > 0) { object colval = prop.CallGet(o); if (colval == null) { try { colval = prop.PropertyType.CreateNewChildInstance(o); prop.CallSet(o, colval); } catch { colval = null; } } if (colval != null) { foreach (XmlElement elem in lst) { if (attr.ElemType == typeof(string)) { ((IList)colval).Add(elem.InnerText); } else if (attr.ElemType == typeof(NameWithSchema)) { ((IList)colval).Add(NameWithSchema.LoadFromXml(elem)); } else { object item; item = attr.ElemType.CreateNewChildInstance(o); item.LoadProperties(elem); ((IList)colval).Add(item); } } } } } if (propHandled) { continue; } object val = PropertyFromString(prop, sval); if (val != null) { prop.CallSet(o, val); } //mtd.Invoke(o, new object[] { val }); } }
public DataSynTableOrViewSource(XmlElement xml) : base(xml) { Name = NameWithSchema.LoadFromXml(xml.FindElement("Name")); }