Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- // using System.Xml;
- using System.Xml.Linq;
- namespace ConsoleApp1
- {
- class Position
- {
- public int x { get; set; }
- public int y { get; set; }
- public override string ToString()
- {
- return string.Format("(x = {0}, y = {1})", x, y);
- }
- }
- class GameObject
- {
- public string Name { get; set; }
- public Position Position { get; set; }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var objects = new GameObject[]
- {
- new GameObject { Name = "item 1", Position = new Position{x = 1, y = 2 } },
- new GameObject { Name = "item 2", Position = new Position{x = 1, y = 2 } },
- new GameObject { Name = "item 3", Position = new Position{x = 1, y = 2 } },
- new GameObject { Name = "item 4", Position = new Position{x = 1, y = 2 } },
- new GameObject { Name = "item 5", Position = new Position{x = 1, y = 2 } },
- };
- var doc = new XDocument(
- new XElement("root",
- from p in objects select new XElement("gameobject",
- new XAttribute("name", p.Name),
- new XElement("position", new XAttribute("x", p.Position.x), new XAttribute("y", p.Position.y))))
- );
- Console.WriteLine(doc.ToString());
- var read = from d in doc.Descendants("gameobject")
- let position = d.Element("position")
- select new GameObject {
- Name = d.Attribute("name").Value,
- Position = new Position
- {
- x = Convert.ToInt32(position.Attribute("x").Value),
- y = Convert.ToInt32(position.Attribute("y").Value),
- }
- };
- foreach (var go in read)
- {
- Console.WriteLine(string.Format("name = {0}, position = {1}", go.Name, go.Position));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement