。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApplication8
{
public class Player
{
public string Name { get; set; }
public int level { get; set; }
public int cash { get; set; }
}
class Program
{
static void Main(string[] args)
{
// you may have several players like below
List<Player> Players = new List<Player>() {
new Player() { Name = "John", cash = 3, level = 215 },
new Player() { Name = "Mike", level = 7, cash = 780 }
};
// save them to Xml file
Save("players.xml", Players);
//when you need details of a given player "Mike"
Player PlayersMike = Load("players.xml", "Mike");
// Adding new player
AddPlayer("players.xml", new Player() { Name = "Test", level = 1, cash = 780 });
}
/// <summary>
/// Saves the specified XML file with players data
/// </summary>
/// <param name="xmlFile">The XML file.</param>
/// <param name="Players">The players.</param>
public static void Save(string xmlFile, List<Player> Players)
{
XElement xml = new XElement("Players",
from p in Players
select new XElement("Player",
new XElement("Name", p.Name),
new XElement("level", p.level),
new XElement("cash", p.cash)));
xml.Save(xmlFile);
}
/// <summary>
/// Loads the specified XML file.
/// </summary>
/// <param name="xmlFile">The XML file.</param>
/// <returns></returns>
public static Player Load(string xmlFile, string name)
{
XDocument doc = XDocument.Load(xmlFile);
var query = (from xElem in doc.Descendants("Player")
where xElem.Element("Name").Value.Equals(name)
select new Player
{
Name = xElem.Element("Name").Value,
level = int.Parse(xElem.Element("level").Value),
cash = int.Parse(xElem.Element("cash").Value),
}).FirstOrDefault();
return query;
}
/// <summary>
/// Adds the player.
/// </summary>
/// <param name="xmlFile">The XML file.</param>
/// <param name="p">The p.</param>
public static void AddPlayer(string xmlFile, Player p)
{
XDocument doc = XDocument.Load(xmlFile);
doc.Element("Players").Add(
new XElement("Player",
new XElement("Name", p.Name),
new XElement("level", p.level),
new XElement("cash", p.cash)));
doc.Save(xmlFile);
}
}
}
まず、新しいXmlTextReader()を使用しないでください。 'XmlReader.Create()'を使用してください。第二に、 'XmlReader'はXMLを読むのが最も難しい方法です。 XMLにLINQを使用していない、または少なくとも 'XmlDocument'を使用している理由はありますか? –
私はLINQを使用していない理由はありません。ちょうどそれに切り替えましたが、Googleから明確なチュートリアルは見つかりませんでした。情報を取得する方法については、私は書くことができます:/ –
Googleの使用を中止することをお勧めしますベンダーが既に文書化している科目に関するチュートリアル。 [System.Xml.Linq名前空間](http://msdn.microsoft.com/en-us/library/bb299195.aspx)を参照してください。 XMLを読み込むには、['XDocument.Load'](http://msdn.microsoft.com/en-us/library/bb358684.aspx)を使用してください。 –