/// <summary> /// Пробегаемся по всем звеньям xml и сливаем их в одно общее правило /// </summary> /// <param name="xmlResult">Результирующая xml</param> /// <param name="xmlCurrent">Текущая xml</param> private static void XmlNodeProceed(XmlNode xmlResult, XmlNode xmlCurrent) { foreach (XmlNode xmlNode in xmlCurrent.ChildNodes) { if (!xmlNode.HasChildNodes) { if (xmlNode.ParentNode == null) { continue; } var propertie = MainSettings.GetPropertie(xmlNode.ParentNode.Name); if (propertie != null && MainSettings.CanMergeProperties(propertie.Name)) { xmlResult.InnerText = MainSettings.MergeValues(xmlResult.InnerText, xmlNode.InnerText); } } else { var resultNode = xmlResult.SelectSingleNode(xmlNode.Name); if (resultNode == null) { if (xmlResult.OwnerDocument == null) { continue; } var xmlNew = xmlResult.OwnerDocument.CreateElement(xmlNode.Name); xmlNew.InnerXml = xmlNode.InnerXml; xmlResult.AppendChild(xmlNew); continue; } XmlNodeProceed(resultNode, xmlNode); } } }
/// <summary> /// Сохранить свойства в виде xml /// </summary> /// <param name="instance">Объект элемента</param> /// <param name="name">Название элемента</param> public static XElement PropertiesToXml(this object instance, string name = null) { if (string.IsNullOrEmpty(name)) { name = instance.GetType() .GetProperty("Name") .GetValue(instance, null) .ToString(); } if (string.IsNullOrEmpty(name)) { return(null); } var element = new XElement(name); var properties = instance.GetType().GetProperties(); foreach (var property in properties) { var visiblePropertie = MainSettings.GetPropertie(property.Name); if (visiblePropertie == null) { continue; } element.Add(visiblePropertie.IsComplex ? new XElement(property.GetValue(instance, null).PropertiesToXml(visiblePropertie.Name)) : new XElement(property.Name, property.GetValue(instance, null))); } return(element); }
/// <summary> /// Загрузить свойства объекта из xml /// </summary> /// <param name="instance">Элемент управления</param> /// <param name="xml">Xml c настройками</param> /// <param name="name">Название компонента</param> public static void PropertyFromXml(this object instance, XmlDocument xml, string name = null) { if (xml == null) { return; } if (name == null) { name = instance.GetType() .GetProperty("Name") .GetValue(instance, null) .ToString(); } foreach (var xmlNode in xml.GetElementsByTagName(name) .Cast <XmlNode>() .Where(node => node != null)) { var properties = instance.GetType() .GetProperties(); foreach (var property in properties) { try { var visiblePropertie = MainSettings.GetPropertie(property.Name); if (visiblePropertie == null || xmlNode[property.Name] == null) { continue; } if (visiblePropertie.IsComplex) { var xmldoc = new XmlDocument(); xmldoc.LoadXml(xmlNode.OuterXml); property.GetValue(instance, null).PropertyFromXml(xmldoc, property.Name); } else { var typeConverter = TypeDescriptor.GetConverter(property.PropertyType); var value = typeConverter.ConvertFromString(xmlNode[property.Name].InnerText); property.SetValue(instance, value, null); } } catch (Exception ex) { DBException.WriteLog(ex); } } } }