1、定义
XML,指可拓展标记语言。被设计用来传输和存储数据。
2、语法规则
3、命名规则
4、C#中进行XML操作
1、在VS中新建一个文本文档“PlayerSkills”;
2、右键选择打开方式为“XML(文本)编辑器”;
3、进行xml操作,包括查询、增加、修改、删除和保存;
<skills><skill01><id>01</id><name lang=\"china\">无情冲锋</name><damage>55</damage></skill01><skill02><id>02</id><name lang=\"china\">背水一战</name><damage>80</damage></skill02><skill03><id>03</id><name lang=\"english\">guoshiwushaung</name><damage>140</damage></skill03></skills>
using System;using System.Collections.Generic;using System.Xml;/** XML操作* 2020/4/12* 引入XML* 使用XmlDocument解析xml文档*/namespace XML操作{class Program{static void Main(string[] args){//创建技能信息列表,用来存储所有技能信息List<Skill> skillList = new List<Skill>();//XmlDocument专门用来解析xml文档XmlDocument xmlDoc = new XmlDocument();//选择要加载解析的xml文档xmlDoc.Load(\"PlayerSkills.txt\");//XmlNode用来代表一个节点XmlNode rootNote = xmlDoc.FirstChild; //得到根节点//得到根节点下面的子节点的集合//ChildNodes获取当前节点下面的所有子节点XmlNodeList skillNoteList = rootNote.ChildNodes;foreach (XmlNode killNode in skillNoteList){Skill skill = new Skill();//获取fieidNode下的所有子节点XmlNodeList fieldNodeList = killNode.ChildNodes;foreach(XmlNode fieidNode in fieldNodeList){if (fieidNode.Name == \"id\"){//fieidNode.InnerText获取节点内部文本skill.ID = int.Parse(fieidNode.InnerText);}else if (fieidNode.Name == \"name\"){skill.Name = fieidNode.InnerText;//获取节点的属性skill.Lang = fieidNode.Attributes[0].Value;}else if (fieidNode.Name == \"damage\"){skill.Damage= int.Parse(fieidNode.InnerText);}}skillList.Add(skill);}foreach(Skill skillInfo in skillList){Console.WriteLine(skillInfo);}Console.ReadKey();}}}
namespace XML操作{class Skill{public int ID { get; set; }public string Name { get; set; }public string Lang { get; set; }public int Damage { get; set; }public override string ToString(){return string.Format(\"ID:{0}, Name:{1},Lang:{2}, Damage:{3}\",ID,Name,Lang,Damage);}}}