一.在工程目录下,新建txt文件取名Skills,修改其属性运行时复制到工作目录中;
右键选择Open with
选择Xml方便编辑txt内容
二.新建一个SkillInfo类,用来存储Skill信息
class SkillInfo { public int ID { get; set; } public string Name { get; set; } public int Damage { set; get; } public string Language { get; set; } public override string ToString() { return string.Format("ID:" + ID + ",Name:" + Name + ",Damage:" + Damage + ",Language:" + Language); } }
static void Main(string[] args) { List<SkillInfo> SkillList = new List<SkillInfo>(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("Skills.txt"); XmlNode skills = xmlDoc.FirstChild; XmlNodeList skillList = skills.ChildNodes; foreach (XmlNode skill in skillList) { XmlNodeList skillInfo = skill.ChildNodes; SkillInfo skillObject = new SkillInfo(); foreach (XmlNode info in skillInfo) { if (info.Name== "ID") { skillObject.ID =int.Parse(info.InnerText); } if (info.Name == "Name") { skillObject.Name = info.InnerText; skillObject.Language = info.Attributes[0].Value; } if (info.Name == "Damage") { skillObject.Damage = int.Parse(info.InnerText); } } SkillList.Add(skillObject); } foreach(SkillInfo list in SkillList) { Console.WriteLine(list.ToString()); } Console.ReadKey(); }
Skills.txt文本编写格式如下:
结果如下: