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.Xml;
- using System.Xml.Linq;
- using System.IO;
- using System.Threading.Tasks;
- namespace XMLPracticeC
- {
- class Program
- {
- static void Main()
- {
- //using xmlDocument
- string path = @"\\bgu-file\student-desktops$\15cottlefi\Desktop\VBFinCottle\XMLPracticeC\XMLPracticeC\bin\Debug\Assets\practice.xml";
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNodeList aNodes = doc.GetElementsByTagName("user");
- foreach (XmlNode aNode in aNodes)
- {
- XmlAttribute nameAttribute = aNode.Attributes["name"];
- Console.WriteLine(nameAttribute.Value);
- }
- doc.Save(path);
- //using xmlLinq
- Read(path);
- Console.WriteLine("\nPress any key to continue...");
- Console.ReadKey();
- }
- //using Xml Linq to read all statistic values
- static void Read(string fileName)
- {
- XDocument doc = XDocument.Load(fileName);
- //foreach (XElement user in doc.Root.Elements())
- //{
- // Console.WriteLine("{0} {1}", user.Name, user.Attribute("name").Value);
- // Console.WriteLine(" \nAttributes:");
- // foreach (XAttribute attr in user.Attributes())
- // Console.WriteLine(" {0}", attr);
- // Console.WriteLine(" \nElements:");
- // foreach (XElement element in user.Elements())
- // Console.WriteLine(" {0}: {1}", element.Name, element.Value);
- // Console.WriteLine(user.Attribute("name").Value); //prints "Fin" or "Player1" useful for later
- //}
- createUser("Fin");
- createUser("Jack");
- }
- static void createUser(string username)
- {
- XDocument doc = XDocument.Load("createPractice.xml");
- int roundsWon = 10; //debug
- int roundsLost = 5; //debug
- List<string> list = new List<string>
- {
- "Data1", "Data2", "Data3"
- };
- doc.Elements("statistics").First().Add(
- new XElement("name", new XAttribute("filename", "statsFile")),
- new XElement("date", new XAttribute("modified", DateTime.Now)),
- new XElement("users",
- new XElement("user", new XAttribute("username", username),
- new XElement("rounds",
- new XElement("roundsWon", roundsWon),
- new XElement("roundsLost", roundsLost)),
- new XElement("sets",
- new XElement("setsWon", 34),
- new XElement("setsLost", 32))
- )
- )
- );
- doc.Save("createPractice.xml");
- Console.WriteLine("File saved");
- }
- static void createFile()
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement