Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. // using System.Xml;
  4. using System.Xml.Linq;
  5.  
  6. namespace ConsoleApp1
  7. {
  8. class Position
  9. {
  10. public int x { get; set; }
  11. public int y { get; set; }
  12. public override string ToString()
  13. {
  14. return string.Format("(x = {0}, y = {1})", x, y);
  15. }
  16. }
  17. class GameObject
  18. {
  19. public string Name { get; set; }
  20. public Position Position { get; set; }
  21.  
  22. }
  23. class Program
  24. {
  25. static void Main(string[] args)
  26. {
  27. var objects = new GameObject[]
  28. {
  29. new GameObject { Name = "item 1", Position = new Position{x = 1, y = 2 } },
  30. new GameObject { Name = "item 2", Position = new Position{x = 1, y = 2 } },
  31. new GameObject { Name = "item 3", Position = new Position{x = 1, y = 2 } },
  32. new GameObject { Name = "item 4", Position = new Position{x = 1, y = 2 } },
  33. new GameObject { Name = "item 5", Position = new Position{x = 1, y = 2 } },
  34. };
  35.  
  36. var doc = new XDocument(
  37. new XElement("root",
  38. from p in objects select new XElement("gameobject",
  39. new XAttribute("name", p.Name),
  40. new XElement("position", new XAttribute("x", p.Position.x), new XAttribute("y", p.Position.y))))
  41. );
  42.  
  43. Console.WriteLine(doc.ToString());
  44.  
  45. var read = from d in doc.Descendants("gameobject")
  46. let position = d.Element("position")
  47. select new GameObject {
  48. Name = d.Attribute("name").Value,
  49. Position = new Position
  50. {
  51. x = Convert.ToInt32(position.Attribute("x").Value),
  52. y = Convert.ToInt32(position.Attribute("y").Value),
  53. }
  54. };
  55. foreach (var go in read)
  56. {
  57. Console.WriteLine(string.Format("name = {0}, position = {1}", go.Name, go.Position));
  58. }
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement