Reputation: 51
I can't find a proper solution anywhere that I can properly understand. So if you can explain what to do, that would be great. I'm writing a program that can read data from save file in a video game.
How can I write something in C# that will save the integers under professions into an array? I'm using XDocument right now with a variable that points to the "player" element as a root.
var player = doc.Root.Element("player");
My initial thought was to create a variable with the profession as the root and use a forloop to save all the values of the elements within professions to an array, but I have no idea how to start that
For context this is what the XML looks like (I added some periods to show that there are other elements):
<?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">
<player>
.
.
.
<professions>
<int>1</int>
<int>18</int>
<int>25</int>
<int>13</int>
<int>4</int>
<int>6</int>
</professions>
.
.
.
</player>
</SaveGame>
Upvotes: 0
Views: 1115
Reputation: 34421
using 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; }
}
}
You can also use xml serializer
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; }
}
}
Code below deserializes the xml file
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; }
}
}
Upvotes: 1