Exemplo n.º 1
0
        public static bool UpdateByXML(UserModel p, string xmlFilename)
        {
            GenericPropertyFinder <UserModel> objGenericPropertyFinder = new GenericPropertyFinder <UserModel>();
            var lstDic = objGenericPropertyFinder.ReturTModelPropertyAndValue(p);

            XDocument xdoc = XDocument.Load(xmlFilename);

            xdoc.Declaration = new XDeclaration("1.0", "utf-8", "yes");
            var elements = xdoc.Element("ArrayOfUserModel").Elements();

            var valueProdID = (from x in lstDic
                               where x.Key.Contains("Guid")
                               select x.Value).FirstOrDefault();

            foreach (var child in elements)
            {
                var elmId = child.Element("Guid");
                if (elmId.Value == valueProdID)
                {
                    foreach (var item in child.Elements())
                    {
                        if (item.Name == "_Role")
                        {
                            GenericPropertyFinder <RoleModel> objRole = new GenericPropertyFinder <RoleModel>();

                            var pItems = objRole.ReturTModelPropertyAndValue(p._Role);

                            foreach (var subItem in item.Elements())
                            {
                                var valueOf = (from x in pItems
                                               where x.Key.Contains(subItem.Name.ToString())
                                               select x.Value).FirstOrDefault();

                                subItem.SetValue(valueOf);
                            }
                        }
                        else if (item.Name == "Depto")
                        {
                            GenericPropertyFinder <DeptoModel> objDepto = new GenericPropertyFinder <DeptoModel>();

                            var pItems = objDepto.ReturTModelPropertyAndValue(p.Depto);

                            foreach (var subItem in item.Elements())
                            {
                                var valueOf = (from x in pItems
                                               where x.Key.Contains(subItem.Name.ToString())
                                               select x.Value).FirstOrDefault();

                                subItem.SetValue(valueOf);
                            }
                        }
                        else
                        {
                            var valueOf = (from x in lstDic
                                           where x.Key.Contains(item.Name.ToString())
                                           select x.Value).FirstOrDefault();

                            if (valueOf != null)
                            {
                                item.SetValue(valueOf);
                            }
                        }
                    }
                }
                else
                {
                    continue;
                }
            }

            xdoc = ValidatesFieldsXDoc(xdoc);

            xdoc.Save(xmlFilename);
            return(true);
        }
Exemplo n.º 2
0
        public static bool AddByXML(UserModel p, string xmlFilename)
        {
            XDocument xdoc = XDocument.Load(xmlFilename);

            if (p.UserId <= 0)
            {
                p.UserId = xdoc.Root.Elements().Count() + 1;
            }
            p.Guid = Guid.NewGuid();

            GenericPropertyFinder <UserModel> objGenericPropertyFinder = new GenericPropertyFinder <UserModel>();
            var lstDic = objGenericPropertyFinder.ReturTModelPropertyAndValue(p);

            XElement ele    = new XElement("UserModel", null);
            XElement subEle = null;

            foreach (var item in lstDic)
            {
                if (subEle != null && subEle.Name != item.Key.Remove(5))
                {
                    ele.Add(subEle);
                    subEle = null;
                }

                if (item.Key.Contains("_Role"))
                {
                    if (subEle == null)
                    {
                        subEle = new XElement("_Role", null);
                    }


                    var valueOf = (from x in lstDic
                                   where x.Key.Contains(item.Key)
                                   select x.Value).FirstOrDefault();

                    subEle.SetElementValue(item.Key.Remove(0, 6), valueOf);
                    continue;
                }
                else if (item.Key.Contains("Depto"))
                {
                    if (subEle == null)
                    {
                        subEle = new XElement("Depto", null);
                    }

                    var valueOf = (from x in lstDic
                                   where x.Key.Contains(item.Key)
                                   select x.Value).FirstOrDefault();

                    subEle.SetElementValue(item.Key.Remove(0, 6), valueOf);
                    continue;
                }
                else
                {
                    var valueOf = (from x in lstDic
                                   where x.Key.Contains(item.Key)
                                   select x.Value).FirstOrDefault();

                    ele.SetElementValue(item.Key, valueOf);
                }
            }
            xdoc.Root.Add(ele);

            xdoc = ValidatesFieldsXDoc(xdoc);

            xdoc.Save(xmlFilename);
            return(true);
        }