Esempio n. 1
0
 internal static void DictionaryFromXmlNodeList <T>(this Dictionary <string, T> items, XmlNodeList nodelist, string ElementName)
 {
     foreach (XmlElement ele in nodelist)
     {
         var            name        = ele.Name;
         var            key         = ele.Attributes["key"].InnerText;
         var            obj         = EzObject.CreateInstance(ele.Name);
         IXmlRenderable valToRender = obj as IXmlRenderable;
         if (valToRender != null)
         {
             ((IXmlRenderableInternal)valToRender).FromXml(ele);
             var e = (T)valToRender;
             items.Add(key, e);
         }
     }
 }
Esempio n. 2
0
 internal static void ListFromXmlNodeList <T>(this List <T> items, XmlNodeList nodelist, string ElementName)
 {
     foreach (XmlElement ele in nodelist)
     {
         var            name        = ele.Name;
         var            obj         = EzObject.CreateInstance(ele.Name);
         IXmlRenderable valToRender = obj as IXmlRenderable;
         if (valToRender != null)
         {
             T e = default(T);
             if ((ele.Attributes["ref"] != null) && (ele.Attributes["ref"].InnerText.Length > 0))
             {
                 e = (T)EzObject.CreateInstance(ele.Name);
                 ((IEzObject)e)._id = (int.Parse(ele.Attributes["ref"].InnerText) * -1);
             }
             else
             {
                 ((IXmlRenderableInternal)valToRender).FromXml(ele);
                 e = (T)valToRender;
             }
             items.Add(e);
         }
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Returns an item as an XML Node
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item">The item.</param>
        /// <param name="_doc">The document.</param>
        /// <param name="ElementName">Name of the element.</param>
        /// <param name="KeyAttributeValue">The key attribute value.</param>
        /// <returns></returns>
        internal static void FromXmlNode <T>(this T item, XmlNode node, string ElementName)
        {
            try
            {
                if (node.Name.Equals(ElementName))
                {
                    foreach (XmlElement ele in node.ChildNodes)
                    {
                        try
                        {
                            var pi = item.GetType().GetProperty(ele.Name);
                            if (pi != null)
                            {
                                var refId        = (ele.Attributes["ref"] != null ? ele.Attributes["ref"].InnerText : null);
                                var target       = pi.GetValue(item, null);
                                var isNullable   = (Nullable.GetUnderlyingType(pi.PropertyType) != null);
                                var propertyType = (!isNullable ? pi.PropertyType : Nullable.GetUnderlyingType(pi.PropertyType));
                                if (!string.IsNullOrEmpty(refId))
                                {
                                    //Create a negative reference id so we will come back and resolve this later
                                    var o = (IEzObject)EzObject.CreateInstance(propertyType.Name);
                                    o._id = int.Parse(refId) * -1;
                                    pi.SetValue(item, o);
                                }
                                else
                                {
                                    var val = ele.InnerText;

                                    if (((propertyType.FullName.Contains("DateTime") || propertyType.Name.Equals("String") || propertyType.IsPrimitive)) && (!string.IsNullOrWhiteSpace(val)))
                                    {
                                        if (propertyType.FullName.Contains("DateTime"))
                                        {
                                            DateTime value = DateTime.Parse(val);
                                            pi.SetValue(item, value);
                                        }
                                        else
                                        {
                                            var value = Convert.ChangeType(ele.InnerText, pi.PropertyType);
                                            pi.SetValue(item, value);
                                        }
                                    }
                                    else if (target != null)
                                    {
                                        IXmlRenderable valToRender = target as IXmlRenderable;
                                        if (valToRender != null)
                                        {
                                            try
                                            {
                                                ((IXmlRenderableInternal)valToRender).FromXml(ele);
                                            }
                                            catch (Exception)
                                            {
                                                throw;
                                            }
                                        }
                                    }
                                }
                                if (ele.Name.Equals("_id"))
                                {
                                    ((IEzObject)item).SetRefId();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw;
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }