Advertisement
Guest User

XmlReadWrite

a guest
Sep 27th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7.  
  8. namespace wpfInstructie
  9. {
  10. class xmlReadWrite
  11. {
  12. private XmlDocument getDB(string xmlName)
  13. {
  14. XmlDocument doc = new XmlDocument();
  15. try
  16. {
  17. doc.Load(getXMLLocation(xmlName));
  18. }
  19. catch { doc = null; }
  20. if (doc == null)
  21. {
  22. doc = new XmlDocument();
  23. XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  24. XmlElement root = doc.CreateElement(string.Empty, xmlName, null);
  25. doc.AppendChild(root);
  26. doc.Save(getXMLLocation(xmlName));
  27. }
  28.  
  29. return doc;
  30. }
  31.  
  32. internal List<Comment> getAllComments()
  33. {
  34. List<Comment> comments = new List<Comment>();
  35. foreach (XmlNode node in getDB("Comment").DocumentElement.ChildNodes)
  36. comments.Add(new Comment(node.SelectSingleNode("Commenttext").InnerText, getUserByName(node.SelectSingleNode("Name").InnerText)));
  37. return comments;
  38. }
  39.  
  40. private User getUserByName(string name)
  41. {
  42. foreach (XmlNode node in getDB("User").DocumentElement.ChildNodes)
  43. if (node.SelectSingleNode("Name").InnerText == name)
  44. return new User(node.SelectSingleNode("Name").InnerText, node.SelectSingleNode("Pass").InnerText, node.SelectSingleNode("Email").InnerText);
  45. return null;
  46. }
  47.  
  48. internal void saveNewComment(Comment comment)
  49. {
  50. XmlDocument doc = getDB("Comment");
  51.  
  52. XmlNode newComment = doc.CreateNode(XmlNodeType.Element, "Comment", null);
  53. doc.DocumentElement.AppendChild(newComment);
  54.  
  55. XmlNode commentText = doc.CreateNode(XmlNodeType.Element, "Commenttext", null);
  56. commentText.InnerText = comment.CommentText;
  57. newComment.AppendChild(commentText);
  58.  
  59. XmlNode name = doc.CreateNode(XmlNodeType.Element, "Name", null);
  60. name.InnerText = comment.Poster.name.ToString();
  61. newComment.AppendChild(name);
  62.  
  63. doc.Save(getXMLLocation("Comment"));
  64. }
  65.  
  66. internal User searchForUser(string username, string password)
  67. {
  68. foreach (XmlNode node in getDB("User").DocumentElement.ChildNodes)
  69. if (node.SelectSingleNode("Name").InnerText == username)
  70. if (node.SelectSingleNode("Pass").InnerText == password)
  71. return new User(node.SelectSingleNode("Name").InnerText, node.SelectSingleNode("Pass").InnerText, node.SelectSingleNode("Email").InnerText);
  72. return null;
  73. }
  74.  
  75. internal void saveNewUser(User user)
  76. {
  77. XmlDocument doc = getDB("User");
  78.  
  79. if (checkForExistence(user.name, "Name", "User"))
  80. return;
  81. if (checkForExistence(user.email, "Email", "User"))
  82. return;
  83.  
  84. XmlNode newUser = doc.CreateNode(XmlNodeType.Element, "User", null);
  85. doc.DocumentElement.AppendChild(newUser);
  86.  
  87. XmlNode name = doc.CreateNode(XmlNodeType.Element, "Name", null);
  88. name.InnerText = user.name.ToString();
  89. newUser.AppendChild(name);
  90.  
  91. XmlNode email = doc.CreateNode(XmlNodeType.Element, "Email", null);
  92. email.InnerText = user.email.ToString();
  93. newUser.AppendChild(email);
  94.  
  95. XmlNode pass = doc.CreateNode(XmlNodeType.Element, "Pass", null);
  96. pass.InnerText = user.password.ToString();
  97. newUser.AppendChild(pass);
  98.  
  99. doc.Save(getXMLLocation("User"));
  100. }
  101. /// <summary>
  102. ///
  103. /// </summary>
  104. /// <param name="value"></param>
  105. /// <param name="field"></param>
  106. /// <param name="xmldoc"></param>
  107. /// <returns></returns>
  108. private bool checkForExistence(string value, string field, string xmldoc)
  109. {
  110. foreach (XmlNode node in getDB(xmldoc).DocumentElement.ChildNodes)
  111. {
  112. if (value == node.SelectSingleNode(field).InnerText)
  113. return true;
  114. }
  115. return false;
  116. }
  117.  
  118. private string getXMLLocation(string xmlName)
  119. {
  120. switch (xmlName)
  121. {
  122. case "Comment":
  123. return Environment.CurrentDirectory + @"/comment.xml";
  124. case "User":
  125. return Environment.CurrentDirectory + @"/user.xml";
  126. default:
  127. throw new MissingMemberException();
  128. // return null;
  129. }
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement