public User GetUser(string nameId)
        {
            XElement userRootElem = XMLTools.LoadListFromXMLElement(usersPath);

            User myUser = (from temUser in userRootElem.Elements()
                           where temUser.Element("UserName").Value == nameId
                           select new User()
            {
                Valid = bool.Parse(temUser.Element("Valid").Value),
                UserName = temUser.Element("UserName").Value,
                Password = temUser.Element("Password").Value,
                Permission = (DO.authority)authority.Parse(typeof(authority), temUser.Element("Permission").Value)
            }
                           ).FirstOrDefault();

            if (myUser == null)
            {
                throw new DO.DOBadUserIdException("", "no user with such UserName!!");
            }
            if (!myUser.Valid)
            {
                throw new Exception("user is not valid!!");
            }
            return(myUser);
        }
        public void UpdateSequentialStopInfoTravelTime(long firstId, long secondId, TimeSpan travelTime)
        {
            XElement userRootElem = XMLTools.LoadListFromXMLElement(sequentialStopInfoPath);

            GetSequentialStopInfo(firstId, secondId);
            XElement mySeq = (from u in userRootElem.Elements()
                              where u.Element("StationCodeF").Value == firstId.ToString() && u.Element("StationCodeS").Value == secondId.ToString()
                              select u).FirstOrDefault();

            if (mySeq != null)
            {
                mySeq.Element("TravelTime").Value = XmlConvert.ToString(travelTime);
                XMLTools.SaveListToXMLElement(userRootElem, sequentialStopInfoPath);
            }
        }
        public void DeleteSequentialStopInfo(long firstId, long secondId)
        {
            XElement userRootElem = XMLTools.LoadListFromXMLElement(sequentialStopInfoPath);

            GetSequentialStopInfo(firstId, secondId);
            XElement mySeq = (from u in userRootElem.Elements()
                              where u.Element("StationCodeF").Value == firstId.ToString() && u.Element("StationCodeS").Value == secondId.ToString()
                              select u).FirstOrDefault();

            if (mySeq != null)
            {
                mySeq.Element("Valid").Value = false.ToString();
                XMLTools.SaveListToXMLElement(userRootElem, sequentialStopInfoPath);
            }
        }
        /// <summary>
        /// request a User according to a predicate
        /// </summary>
        /// <param name="pr"></param>
        /// <returns></returns>

        public User RequestUser(Predicate <DO.User> pr = null)
        {
            XElement userRootElem = XMLTools.LoadListFromXMLElement(usersPath);
            var      tem          = from temUser in userRootElem.Elements()
                                    let u1 = new User()
            {
                Valid      = bool.Parse(temUser.Element("Valid").Value),
                UserName   = temUser.Element("UserName").Value,
                Password   = temUser.Element("Password").Value,
                Permission = (DO.authority)authority.Parse(typeof(authority), temUser.Element("Permission").Value)
            }
            where pr(u1)
            select u1;

            return(tem.FirstOrDefault());
        }
        public SequentialStopInfo RequestSequentialStopInfo(Predicate <SequentialStopInfo> pr)
        {
            XElement userRootElem = XMLTools.LoadListFromXMLElement(sequentialStopInfoPath);
            var      tem          = from temSeq in userRootElem.Elements()
                                    let u1 = new SequentialStopInfo()
            {
                Valid        = bool.Parse(temSeq.Element("Valid").Value),
                StationCodeF = long.Parse(temSeq.Element("StationCodeF").Value),
                StationCodeS = long.Parse(temSeq.Element("StationCodeS").Value),
                Distance     = double.Parse(temSeq.Element("Distance").Value),
                TravelTime   = XmlConvert.ToTimeSpan(temSeq.Element("TravelTime").Value.ToString())
            }
            where pr(u1)
            select u1;

            return(tem.FirstOrDefault());
        }
        public IEnumerable <SequentialStopInfo> GetAllSequentialStopInfo()
        {
            XElement userRootElem = XMLTools.LoadListFromXMLElement(sequentialStopInfoPath);

            var temList = new List <User>();

            IEnumerable <SequentialStopInfo> temp = (from temSeq in userRootElem.Elements()
                                                     select new SequentialStopInfo()
            {
                Valid = bool.Parse(temSeq.Element("Valid").Value),
                StationCodeF = long.Parse(temSeq.Element("StationCodeF").Value),
                StationCodeS = long.Parse(temSeq.Element("StationCodeS").Value),
                Distance = double.Parse(temSeq.Element("Distance").Value),
                TravelTime = XmlConvert.ToTimeSpan(temSeq.Element("TravelTime").Value.ToString())
            });

            return(temp.Where(s => s.Valid == true));
        }
        public void CreateSequentialStopInfo(SequentialStopInfo sequentialStopInfo)
        {
            XElement userRootElem = XMLTools.LoadListFromXMLElement(sequentialStopInfoPath);

            sequentialStopInfo.Valid = true;

            try
            {
                GetSequentialStopInfo(sequentialStopInfo.StationCodeF, sequentialStopInfo.StationCodeS);
            }

            catch (Exception ex)
            {
                if (ex.Message == "no SequentialStopInfo with such license number!!")
                {
                    XElement sequentialStopInfoElem = new XElement("SequentialStopInfo",
                                                                   new XElement("Valid", sequentialStopInfo.Valid.ToString()),
                                                                   new XElement("StationCodeF", sequentialStopInfo.StationCodeF),
                                                                   new XElement("StationCodeS", sequentialStopInfo.StationCodeS),
                                                                   new XElement("Distance", sequentialStopInfo.Distance),
                                                                   new XElement("AverageTime", ""),
                                                                   new XElement("TravelTime", XmlConvert.ToString(sequentialStopInfo.TravelTime)));

                    userRootElem.Add(sequentialStopInfoElem);
                    XMLTools.SaveListToXMLElement(userRootElem, sequentialStopInfoPath);
                }
                else if (ex.Message == "SequentialStopInfoList is not valid!!")
                {
                    XElement per = (from temSeq in userRootElem.Elements()
                                    where temSeq.Element("StationCodeF").Value == sequentialStopInfo.StationCodeF.ToString() && temSeq.Element("StationCodeS").Value == sequentialStopInfo.StationCodeS.ToString()
                                    select temSeq).FirstOrDefault();

                    per.Element("Valid").Value = sequentialStopInfo.Valid.ToString();

                    XMLTools.SaveListToXMLElement(userRootElem, sequentialStopInfoPath);
                }
                return;
            }
            throw new Exception("sequentialStopInfo already exists!!!");
        }
        /// <summary>
        /// add new user to database
        /// </summary>
        /// <param name="user"></param>
        public void CreateUser(User user)
        {
            XElement userRootElem = XMLTools.LoadListFromXMLElement(usersPath);

            user.Valid = true;

            try
            {
                // GetUser(user.Password);
                GetUser(user.UserName);
            }
            catch (Exception ex)
            {
                if (ex.Message == "no user with such UserName!!")
                {
                    XElement userElem = new XElement("User",
                                                     new XElement("Valid", user.Valid.ToString().ToLower()),
                                                     new XElement("UserName", user.UserName),
                                                     new XElement("Password", user.Password),
                                                     new XElement("Permission", user.Permission.ToString()));

                    userRootElem.Add(userElem);
                    XMLTools.SaveListToXMLElement(userRootElem, usersPath);
                }

                else if (ex.Message == "user is not valid!!")
                {
                    XElement per = (from temUser in userRootElem.Elements()
                                    where temUser.Element("UserName").Value == user.UserName
                                    select temUser).FirstOrDefault();

                    per.Element("Valid").Value = user.Valid.ToString();


                    XMLTools.SaveListToXMLElement(userRootElem, usersPath);
                }
                return;
            }
            throw new Exception("user already exists!!!");
        }
        /// <summary>
        /// update password in database
        /// </summary>
        /// <param name="password"></param>
        /// <param name="nameId"></param>
        public void UpdatePassword(string password, string nameId)
        {
            XElement userRootElem = XMLTools.LoadListFromXMLElement(usersPath);

            XElement myUser = (from u in userRootElem.Elements()
                               where u.Element("UserName").Value == nameId
                               select u).FirstOrDefault();

            if (myUser.Element("Valid").Value == "false")
            {
                throw new Exception("user is not valid!!");
            }

            if (myUser == null)
            {
                throw new Exception("no user with such UserName!!");
            }

            if (myUser != null)
            {
                myUser.Element("Password").Value = password;
                XMLTools.SaveListToXMLElement(userRootElem, usersPath);
            }
        }