Advertisement
Guest User

XML Practice c#

a guest
Jul 17th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Xml.Linq;
  7. using System.IO;
  8. using System.Threading.Tasks;
  9.  
  10. namespace XMLPracticeC
  11. {
  12. class Program
  13. {
  14.  
  15.  
  16.  
  17. static void Main()
  18. {
  19. //using xmlDocument
  20. string path = @"\\bgu-file\student-desktops$\15cottlefi\Desktop\VBFinCottle\XMLPracticeC\XMLPracticeC\bin\Debug\Assets\practice.xml";
  21. XmlDocument doc = new XmlDocument();
  22. doc.Load(path);
  23.  
  24. XmlNodeList aNodes = doc.GetElementsByTagName("user");
  25. foreach (XmlNode aNode in aNodes)
  26. {
  27. XmlAttribute nameAttribute = aNode.Attributes["name"];
  28.  
  29. Console.WriteLine(nameAttribute.Value);
  30. }
  31. doc.Save(path);
  32.  
  33. //using xmlLinq
  34. Read(path);
  35.  
  36. Console.WriteLine("\nPress any key to continue...");
  37. Console.ReadKey();
  38. }
  39.  
  40. //using Xml Linq to read all statistic values
  41. static void Read(string fileName)
  42. {
  43. XDocument doc = XDocument.Load(fileName);
  44.  
  45. //foreach (XElement user in doc.Root.Elements())
  46. //{
  47. // Console.WriteLine("{0} {1}", user.Name, user.Attribute("name").Value);
  48. // Console.WriteLine(" \nAttributes:");
  49. // foreach (XAttribute attr in user.Attributes())
  50. // Console.WriteLine(" {0}", attr);
  51. // Console.WriteLine(" \nElements:");
  52.  
  53. // foreach (XElement element in user.Elements())
  54. // Console.WriteLine(" {0}: {1}", element.Name, element.Value);
  55.  
  56. // Console.WriteLine(user.Attribute("name").Value); //prints "Fin" or "Player1" useful for later
  57. //}
  58. createUser("Fin");
  59. createUser("Jack");
  60. }
  61.  
  62. static void createUser(string username)
  63. {
  64. XDocument doc = XDocument.Load("createPractice.xml");
  65. int roundsWon = 10; //debug
  66. int roundsLost = 5; //debug
  67. List<string> list = new List<string>
  68. {
  69. "Data1", "Data2", "Data3"
  70. };
  71.  
  72. doc.Elements("statistics").First().Add(
  73. new XElement("name", new XAttribute("filename", "statsFile")),
  74. new XElement("date", new XAttribute("modified", DateTime.Now)),
  75. new XElement("users",
  76. new XElement("user", new XAttribute("username", username),
  77. new XElement("rounds",
  78. new XElement("roundsWon", roundsWon),
  79. new XElement("roundsLost", roundsLost)),
  80.  
  81. new XElement("sets",
  82. new XElement("setsWon", 34),
  83. new XElement("setsLost", 32))
  84. )
  85. )
  86. );
  87.  
  88. doc.Save("createPractice.xml");
  89. Console.WriteLine("File saved");
  90.  
  91. }
  92.  
  93. static void createFile()
  94. {
  95.  
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement