Beispiel #1
0
 static void Main(string[] args)
 {
     User newUser = new User();
     newUser.SaveAsXml();
     UserFile uf = new UserFile();
     uf.SaveAsXML();
     UserEditor ue = new UserEditor();
     ue.Start();
       
 }
Beispiel #2
0
        // constructors
        public UserFile()
        {
            string filePath = (AppDomain.CurrentDomain.BaseDirectory + (@"\users.xml")); // check current directory (bin) for file

            if (File.Exists(filePath))
            {
                // the file exists, so we can create users from the data
                XDocument document = XDocument.Load("users.xml");
                XElement root;
                IEnumerable<XElement> docElements;
                if (document.Root != null)
                {
                    root = document.Root;
                    if (root.Elements() != null)
                    {
                        docElements = root.Elements();

                        foreach (var d in docElements)
                        {
                            User userName = new User();
                            XElement nameElement = d.Element("Name").IsValidOrDefault();

                            d.Attribute("id").CheckForNull(); // check if value is null before assigning it
                            userName.Id = d.Attribute("id").Value;

                            nameElement.Element("Prefix").IsValidOrDefault(); // check if value is null before assigning it
                            userName.Prefix = nameElement.Element("Prefix").Value;

                            nameElement.Element("FirstName").IsValidOrDefault();  // check if value is null before assigning it
                            userName.FirstName = nameElement.Element("FirstName").Value;

                            nameElement.Element("MiddleName").IsValidOrDefault();  // check if value is null before assigning it
                            userName.MiddleName = nameElement.Element("MiddleName").Value;

                            nameElement.Element("LastName").IsValidOrDefault();  // check if value is null before assigning it
                            userName.LastName = nameElement.Element("LastName").Value;

                            nameElement.Element("Suffix").IsValidOrDefault();  // check if value is null before assigning it
                            userName.Suffix = nameElement.Element("Suffix").Value;

                            d.Element("Username").IsValidOrDefault();  // check if value is null before assigning it
                            userName.Username = d.Element("Username").Value;

                            d.Element("Password").IsValidOrDefault();  // check if value is null before assigning it
                            userName.Password = d.Element("Password").Value;

                            d.Element("DateOfBirth").IsValidOrDefault();  // check if value is null before assigning it (checking when it is a string, before converting)
                            userName.DateOfBirth = DateTime.Parse(d.Element("DateOfBirth").Value);

                            d.Element("JobTitle").IsValidOrDefault();  // check if value is null before assigning it
                            userName.JobTitle = d.Element("JobTitle").Value;

                            userList.Add(userName); //add user to our list of users
                                                    //Console.WriteLine(userList.Count()); //used for testing. Make sure all Users are being added to the list
                        }
                    }
                    else
                    {
                        Console.Error.WriteLine("Error: root.Elements() is null ");
                    }
                }
                Console.Error.WriteLine("Error: document.Root is null ");
            }
            else
            {
                TestXmlData(); //method call to create test data
                //now that we have data, we can create users from the data
                XDocument document = XDocument.Load("users.xml");
                XElement root = document.Root;
                IEnumerable<XElement> docElements = root.Elements();

                foreach (var d in docElements)
                {
                    User userName = new User();

                    XElement nameElement = d.Element("Name").IsValidOrDefault();

                    d.Attribute("id").CheckForNull(); // check if value is null before assigning it
                    userName.Id = d.Attribute("id").Value; // not null - assign value

                    nameElement.Element("Prefix").IsValidOrDefault(); // check if value is null before assigning it
                    userName.Prefix = nameElement.Element("Prefix").Value; // not null - assign value

                    nameElement.Element("FirstName").IsValidOrDefault();  // check if value is null before assigning it
                    userName.FirstName = nameElement.Element("FirstName").Value; // not null - assign value

                    nameElement.Element("MiddleName").IsValidOrDefault();  // check if value is null before assigning it
                    userName.MiddleName = nameElement.Element("MiddleName").Value; // not null - assign value

                    nameElement.Element("LastName").IsValidOrDefault();  // check if value is null before assigning it
                    userName.LastName = nameElement.Element("LastName").Value; // not null - assign value

                    nameElement.Element("Suffix").IsValidOrDefault();  // check if value is null before assigning it
                    userName.Suffix = nameElement.Element("Suffix").Value; // not null - assign value

                    d.Element("Username").IsValidOrDefault();  // check if value is null before assigning it
                    userName.Username = d.Element("Username").Value; // not null - assign value

                    d.Element("Password").IsValidOrDefault();  // check if value is null before assigning it
                    userName.Password = d.Element("Password").Value; // not null - assign value

                    d.Element("DateOfBirth").IsValidOrDefault();  // check if value is null before assigning it (checking when it is a string, before converting)
                    userName.DateOfBirth = DateTime.Parse(d.Element("DateOfBirth").Value); // not null - assign value & do the conversion

                    d.Element("JobTitle").IsValidOrDefault();  // check if value is null before assigning it
                    userName.JobTitle = d.Element("JobTitle").Value; // not null - assign value

                    userList.Add(userName); //add user to our list of users
                    //Console.WriteLine(userList.Count()); //used for testing. Make sure all Users are being added to the list
                }
            }
        }
Beispiel #3
0
        // add a new user
        public void AddUser()
        {
            // for user ID we are going to loop through the list of users and extraxt their IDs. Then, we will set the new user ID to the 
            //// max value + 1
            List<int> userIds = new List<int>();
            var maxId = 0;
            foreach (User usr in usersInfo.userList)
            {
                int tempId = int.Parse(usr.Id);
                userIds.Add(tempId);
                maxId = userIds.Max(); // get max value
                if(tempId > maxId)
                {
                    maxId = tempId;
                }
            }
            consoleUserID = (maxId + 1).ToString(); // set console ID to max value +1 then convert it back to a string for XML

            do // don't move on until field Username has a value
            {
                Console.WriteLine("Please enter a Username: "******"Please enter a Password: "******"Please enter a valid Date of Birth (MM/DD/YYYY): ");
                consoleBday = Console.ReadLine(); // get user input Date of Birth
            }
            // checks to make sure DoB is not null & that DoB is valid
            while (string.IsNullOrWhiteSpace(consoleBday) || (!DateTime.TryParse(consoleBday, out tempDoB)));

            Console.WriteLine("Please enter a Prefix: ");
            string consolePrefix = Console.ReadLine(); // get user input Prefix

            do // don't move on until field FirstName has a value
            {
                Console.WriteLine("Please enter a First Name: ");
                consoleFirstName = Console.ReadLine(); // get user input First Name
            }
            while (string.IsNullOrWhiteSpace(consoleFirstName));

            Console.WriteLine("Please enter a Middle Name: ");
            string consoleMiddleName = Console.ReadLine(); // get user input Middle Name

            do // don't move on until field LastName has a value
            {
                Console.WriteLine("Please enter a Last Name: ");
                consoleLastName = Console.ReadLine(); // get user input Last Name
            }
            while (string.IsNullOrWhiteSpace(consoleLastName));

            Console.WriteLine("Please enter a Suffix: ");
            string consoleSuffix = Console.ReadLine(); // get user input Suffix

            Console.WriteLine("Please enter a Job Title: ");
            string consoleJobTitle = Console.ReadLine(); // get user input JobTitle

            //we have all the values we need, now create a new user
            User newConsoleUser = new User(consoleUserID.ToString(), consolePrefix, consoleFirstName, consoleMiddleName, consoleLastName, 
                consoleSuffix, consoleUserName, consolePassword, consoleBday, consoleJobTitle);

            usersInfo.userList.Add(newConsoleUser); // add this new user to our userList
            usersInfo.SaveAsXML(); // save XML file of all our users
            Console.WriteLine("Saving... User saved successfully!");
            System.Threading.Thread.Sleep(1000);
            ListAllUsers(); // list users to make sure our user was successfully added
        }
Beispiel #4
0
        public User(XElement xele)
        {
            IEnumerable<XElement> usersXele = xele.Elements();
            foreach(var user in usersXele)
            {
                try {
                    User userName = new User();
                    XElement nameElement = user.Element("Name").IsValidOrDefault();

                    user.Attribute("id").CheckForNull();
                    userName.Id = user.Attribute("id").Value;

                    nameElement.Element("Prefix").IsValidOrDefault(); // check if value is null before assigning it
                    userName.Prefix = nameElement.Element("Prefix").Value;

                    nameElement.Element("FirstName").IsValidOrDefault(); // check if value is null before assigning it
                    userName.FirstName = nameElement.Element("FirstName").Value;

                    nameElement.Element("MiddleName").IsValidOrDefault(); // check if value is null before assigning it
                    userName.MiddleName = nameElement.Element("MiddleName").Value;

                    nameElement.Element("LaseName").IsValidOrDefault(); // check if value is null before assigning it
                    userName.LastName = nameElement.Element("LastName").Value;

                    nameElement.Element("Suffix").IsValidOrDefault(); // check if value is null before assigning it
                    userName.Suffix = nameElement.Element("Suffix").Value;

                    user.Element("Username").IsValidOrDefault();  // check if value is null before assigning it
                    userName.Username = user.Element("Username").Value;

                    user.Element("Username").IsValidOrDefault();  // check if value is null before assigning it
                    userName.Password = user.Element("Password").Value;

                    user.Element("DateOfBirth").IsValidOrDefault();  // check if value is null before assigning it (checking when it is a string, before converting)
                    userName.DateOfBirth = DateTime.Parse(user.Element("DateOfBirth").Value);

                    user.Element("JobTitle").IsValidOrDefault();  // check if value is null before assigning it (checking when it is a string, before converting)
                    userName.JobTitle = user.Element("JobTitle").Value;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }