Пример #1
0
        public bool Update(Employee employee)
        {
            XDocument xdoc = XMLActions.ReadXML();
            //XElement root = xdoc.Element("EmployeeList");

            var items = from item in xdoc.Descendants("Employee")
                        where int.Parse(item.Element("ID").Value) == employee.ID
                        select item;

            foreach (XElement itemElement in items)
            {
                itemElement.SetElementValue("Name", employee.Name);
                itemElement.SetElementValue("Experience", employee.Experience);
                itemElement.SetElementValue("Position", employee.Position);
                itemElement.SetElementValue("Salary", employee.Salary);
                itemElement.SetElementValue("Specialization", employee.Specialization);
            }

            return(XMLActions.UpdateXML(xdoc));
        }
Пример #2
0
        public bool Create(Employee employee)
        {
            XDocument xdoc = XMLActions.ReadXML();

            //need to assign new ID, because the ID that may come from the user may be busy already
            var items = (from xe in xdoc.Descendants("Employee")
                         select new
            {
                ID = (string)xe.Element("ID")
            }).ToList();
            int newId;

            //Avoid overflow of int
            if (items.Count() < int.MaxValue)
            {
                newId = items.Count() + 1;
            }
            else
            {
                return(false);
            }

            var tmpId = items.Where(x => x.ID != null).Where(x => x.ID.Contains(newId.ToString())).Select(x => x.ID);

            if (tmpId.Count() < 1)
            {
                employee.ID = newId;
            }
            else
            {
                int ID;
                var LastUsedId = tmpId.OrderBy(x => x).Last();
                //Avoid overflow of int
                if (int.Parse(LastUsedId) < int.MaxValue)
                {
                    ID = int.Parse(LastUsedId) + 1;
                }
                else
                {
                    //We may want to implement logic to find not used ID to correctly handle this situation where we are close to the maximum int value
                    // But as for now we are OK with that for test porposes
                    return(false);
                }

                employee.ID = ID;
            }

            XElement root = xdoc.Element("EmployeeList");

            //Add new employee to XML tree
            root.Add(new XElement("Employee",
                                  new XElement("ID", employee.ID),
                                  new XElement("Name", employee.Name),
                                  new XElement("Experience", employee.Experience),
                                  new XElement("Position", employee.Position),
                                  new XElement("Salary", employee.Salary),
                                  new XElement("Specialization", employee.Specialization)
                                  ));

            return(XMLActions.UpdateXML(xdoc));
        }