VS中,新建项 —添加一个文本文件,右键选择打开方式—Xml文本编辑器,再右键选择 属性 —将复制到输出目录修改为 如果较新则复制。
随便写个Xml文件:
<skills>
<skill>
<id> 1 </id>
<name language ="CN"> 天崩地裂</name>
<damage>66</damage>
</skill>
<skill>
<id> 2</id>
<name language ="EN"> honghonghong</name>
<damage>99</damage>
</skill>
<skill>
<id> 3 </id>
<name language ="CN"> 烈焰冲击</name>
<damage>50</damage>
</skill>
</skills>
创建一个Skills类,重写ToString方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Xml操作
{
class Skills
{
public int ID;
public string Name;
public int Damage;
public string Language;
public override string ToString()
{
return string.Format("ID:{0},Name:{1},Damage:{2},Language:{3}", ID, Name, Damage, Language);
}
}
}
接着上代码解析Xml文档:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Xml操作
{
class Program
{
static void Main(string[] args)
{
//创建技能信息集合 用来存储所有的技能信息
List<Skills> skillList = new List<Skills>();
//XmlDocument 专门用于解析Xml文档
XmlDocument xmlDoc = new XmlDocument();
//加载
xmlDoc.Load("TextFile1.txt");
//根节点
XmlNode rootNode = xmlDoc.FirstChild;
//得到根节点下面的子节点的集合
XmlNodeList skillNodeList = rootNode.ChildNodes;//获取当前节点下所有子节点
foreach (XmlNode skillNode in skillNodeList)
{
Skills skill = new Skills();
XmlNodeList fieldNodeList = skillNode.ChildNodes;//获取skillNode下所有子节点
foreach (XmlNode fieldNode in fieldNodeList)
{
if (fieldNode.Name == "id") //通过Name属性获得节点属性
{
int id = Int32.Parse(fieldNode.InnerText);//获取节点内部文本
skill.ID = id;
}
else if (fieldNode.Name == "name")
{
skill.Name = fieldNode.InnerText;
skill.Language = fieldNode.Attributes[0].Value; //获取属性
}
else
{
skill.Damage = Int32.Parse(fieldNode.InnerText);
}
}
skillList.Add(skill);
}
foreach (var item in skillList)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
Xml解析例子02————Xml技能解析
1.同样是写一个Xml文档
<Skills>
<Skill
skillID ="10010"
skillEngName ="Smash"
TriggerType ="1"
ImageFlie="data/gfx/image/001.dds"
AvailableRange ="10"
>
<Name> 重击</Name>
</Skill>
<Skill
skillID ="10011"
skillEngName ="Hide"
TriggerType ="2"
ImageFlie="data/gfx/image/002.dds"
AvailableRange ="1"
>
<Name> 隐身</Name>
</Skill>
<Skill
skillID ="10012"
skillEngName ="Ikari"
TriggerType ="3"
ImageFlie="data/gfx/image/003.dds"
AvailableRange ="20"
>
<Name> 怒之翼</Name>
</Skill>
</Skills>
2.创建一个Skills类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Xml_技能解析 { class Skills { public string Name; public int skillID; public string skillEngName; public int TriggerType; public string ImageFlie; public int AvailableRange; public override string ToString() { return string.Format("Name:{0},skillID:{1}, skillEngName:{2}, TriggerType:{3}, ImageFlie:{4},AvailableRange:{5}" , Name, skillID, skillEngName, TriggerType, ImageFlie, AvailableRange); } } }
3.解析Xml
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace Xml_技能解析 { class Program { static void Main(string[] args) { List<Skills> skillsList = new List<Skills>(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("SkillText.txt"); XmlNode RootNode = xmlDoc.FirstChild; XmlNodeList skillNodeList = RootNode.ChildNodes; foreach (XmlNode skillNode in skillNodeList) { Skills skill = new Skills(); XmlElement ele = skillNode["Name"]; skill.Name = ele.InnerText; XmlAttributeCollection col = skillNode.Attributes; skill.skillID = Int32.Parse(col["skillID"].Value); skill.TriggerType = Int32.Parse(col["TriggerType"].Value); skill.skillEngName = col["skillEngName"].Value; skill.ImageFlie = col["ImageFlie"].Value; skill.AvailableRange = Int32.Parse(col["AvailableRange"].Value); skillsList.Add(skill); } foreach (var item in skillsList) { Console.WriteLine(item); } Console.ReadKey(); } } }