Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- namespace wpfInstructie
- {
- class xmlReadWrite
- {
- private XmlDocument getDB(string xmlName)
- {
- XmlDocument doc = new XmlDocument();
- try
- {
- doc.Load(getXMLLocation(xmlName));
- }
- catch { doc = null; }
- if (doc == null)
- {
- doc = new XmlDocument();
- XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
- XmlElement root = doc.CreateElement(string.Empty, xmlName, null);
- doc.AppendChild(root);
- doc.Save(getXMLLocation(xmlName));
- }
- return doc;
- }
- internal List<Comment> getAllComments()
- {
- List<Comment> comments = new List<Comment>();
- foreach (XmlNode node in getDB("Comment").DocumentElement.ChildNodes)
- comments.Add(new Comment(node.SelectSingleNode("Commenttext").InnerText, getUserByName(node.SelectSingleNode("Name").InnerText)));
- return comments;
- }
- private User getUserByName(string name)
- {
- foreach (XmlNode node in getDB("User").DocumentElement.ChildNodes)
- if (node.SelectSingleNode("Name").InnerText == name)
- return new User(node.SelectSingleNode("Name").InnerText, node.SelectSingleNode("Pass").InnerText, node.SelectSingleNode("Email").InnerText);
- return null;
- }
- internal void saveNewComment(Comment comment)
- {
- XmlDocument doc = getDB("Comment");
- XmlNode newComment = doc.CreateNode(XmlNodeType.Element, "Comment", null);
- doc.DocumentElement.AppendChild(newComment);
- XmlNode commentText = doc.CreateNode(XmlNodeType.Element, "Commenttext", null);
- commentText.InnerText = comment.CommentText;
- newComment.AppendChild(commentText);
- XmlNode name = doc.CreateNode(XmlNodeType.Element, "Name", null);
- name.InnerText = comment.Poster.name.ToString();
- newComment.AppendChild(name);
- doc.Save(getXMLLocation("Comment"));
- }
- internal User searchForUser(string username, string password)
- {
- foreach (XmlNode node in getDB("User").DocumentElement.ChildNodes)
- if (node.SelectSingleNode("Name").InnerText == username)
- if (node.SelectSingleNode("Pass").InnerText == password)
- return new User(node.SelectSingleNode("Name").InnerText, node.SelectSingleNode("Pass").InnerText, node.SelectSingleNode("Email").InnerText);
- return null;
- }
- internal void saveNewUser(User user)
- {
- XmlDocument doc = getDB("User");
- if (checkForExistence(user.name, "Name", "User"))
- return;
- if (checkForExistence(user.email, "Email", "User"))
- return;
- XmlNode newUser = doc.CreateNode(XmlNodeType.Element, "User", null);
- doc.DocumentElement.AppendChild(newUser);
- XmlNode name = doc.CreateNode(XmlNodeType.Element, "Name", null);
- name.InnerText = user.name.ToString();
- newUser.AppendChild(name);
- XmlNode email = doc.CreateNode(XmlNodeType.Element, "Email", null);
- email.InnerText = user.email.ToString();
- newUser.AppendChild(email);
- XmlNode pass = doc.CreateNode(XmlNodeType.Element, "Pass", null);
- pass.InnerText = user.password.ToString();
- newUser.AppendChild(pass);
- doc.Save(getXMLLocation("User"));
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="value"></param>
- /// <param name="field"></param>
- /// <param name="xmldoc"></param>
- /// <returns></returns>
- private bool checkForExistence(string value, string field, string xmldoc)
- {
- foreach (XmlNode node in getDB(xmldoc).DocumentElement.ChildNodes)
- {
- if (value == node.SelectSingleNode(field).InnerText)
- return true;
- }
- return false;
- }
- private string getXMLLocation(string xmlName)
- {
- switch (xmlName)
- {
- case "Comment":
- return Environment.CurrentDirectory + @"/comment.xml";
- case "User":
- return Environment.CurrentDirectory + @"/user.xml";
- default:
- throw new MissingMemberException();
- // return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement