с использованием Xml Linq:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
List <Player> players = new List<Player>() {
new Player() { professions = new List<Profession>() {
new Profession() { data = new int[]{100, 200, 300, 400}},
new Profession() { data = new int[]{1100, 1200, 1300, 1400}},
new Profession() { data = new int[]{2100, 2200, 2300, 2400}}
}},
new Player() { professions = new List<Profession>() {
new Profession() { data = new int[]{3100, 3200, 3300, 3400}},
new Profession() { data = new int[]{3100, 3200, 3300, 3400}},
new Profession() { data = new int[]{4100, 4200, 4300, 4400}}
}}
};
string ident = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<SaveGame xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
"</SaveGame>";
XDocument doc = XDocument.Parse(ident);
XElement saveGame = doc.Root;
foreach (Player player in players)
{
XElement newPlayer = new XElement("player");
saveGame.Add(newPlayer);
foreach (Profession profession in player.professions)
{
newPlayer.Add(new XElement("professions", profession.data.Select(x => new XElement("int", x))));
}
}
doc.Save(FILENAME);
}
}
public class Player
{
public List<Profession> professions { get; set; }
}
public class Profession
{
public int[] data { get; set; }
}
}
Вы также можете использовать xml сериализатор
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
SaveGame saveGame = new SaveGame();
saveGame.players = new List<Player>() {
new Player() { professions = new List<Profession>() {
new Profession() { data = new int[]{100, 200, 300, 400}},
new Profession() { data = new int[]{1100, 1200, 1300, 1400}},
new Profession() { data = new int[]{2100, 2200, 2300, 2400}}
}},
new Player() { professions = new List<Profession>() {
new Profession() { data = new int[]{3100, 3200, 3300, 3400}},
new Profession() { data = new int[]{3100, 3200, 3300, 3400}},
new Profession() { data = new int[]{4100, 4200, 4300, 4400}}
}}
};
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
ns.Add("xsd", "http://www.w3.org/2001/XMLSchema");
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME, settings);
XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
serializer.Serialize(writer, saveGame, ns);
}
}
public class SaveGame
{
[XmlElement("player")]
public List<Player> players { get; set; }
}
public class Player
{
[XmlElement("profession")]
public List<Profession> professions { get; set; }
}
public class Profession
{
[XmlElement("int")]
public int[] data { get; set; }
}
}
Код ниже десериализует файл xml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
SaveGame sameGame = (SaveGame)serializer.Deserialize(reader);
}
}
public class SaveGame
{
[XmlElement("player")]
public List<Player> players { get; set; }
}
public class Player
{
[XmlElement("profession")]
public List<Profession> professions { get; set; }
}
public class Profession
{
[XmlElement("int")]
public int[] data { get; set; }
}
}