c#簡單使用xml檔案
阿新 • • 發佈:2020-08-03
首先新增txt檔案:
設定為xml開啟模式:
新增根標籤、新增單個標籤和屬性:
<Skills>//根標籤 <skill>//單個標籤 <id>1</id>//屬性 <name lang ="cn">天降正義</name>//lang表示language,cn表示中文,en表示英文 <power>80</power> </skill> <skill> <id>2</id> <name lang ="cn">神來之筆</name> <power>75</power> </skill> <skill> <id>3</id> <name lang="en">doubleHit</name> <power>77</power> </skill> </Skills>
使用xml檔案
方法1:
利用XmlDocment解析xml文件
建立文件內相關資訊的類
選擇要載入解析的xml檔名
得到根節點(根標籤)
得到根節點下的子節點(單個標籤)的集合
遍歷子節點的集合並將子節點下的結點(屬性)的值InnerText賦予相關資訊類的集合,其中也有特性,如上面定義的lang,可以用結點下的Attribute[index].Value得到。
static void Main(string[] args) { List<Skill> SkillList = new List<Skill>(); //使用XmlDocument解析文件 XmlDocument xmlSkillInfo = new XmlDocument(); //兩種方法載入文件 xmlSkillInfo.Load(@".\..\..\SkillInfo.txt");//直接使用檔名載入 //xmlSkillInfo.LoadXml(File.ReadAllText(@".\..\..\SkillInfo.txt"));//使用檔案內的資料載入 //得到根節點(根標籤)//Skills XmlNode rootNode = xmlSkillInfo.FirstChild;//因為當前xml下只有一個根節點,所以用FirstChild獲取即可 //得到根節點下的子節點(單個標籤)//skill XmlNodeList SkillNodeList = rootNode.ChildNodes; //遍歷所有的子節點(skill) foreach (XmlNode skillNode in SkillNodeList) { Skill skill = new Skill(); XmlNodeList skillList = skillNode.ChildNodes; //得到子節點(skill)下的各個屬性(id、name[lang]和power)並將資料存入SkillList foreach (XmlNode fieldNode in skillList) { switch (fieldNode.Name)//通過Name屬性可以獲取結點的名字 { case "id": skill.id = Int32.Parse(fieldNode.InnerText); break; case "name": skill.name = fieldNode.InnerText; skill.lang = fieldNode.Attributes[0].Value; break; case "power": skill.power = Int32.Parse(fieldNode.InnerText); break; default: break; } } SkillList.Add(skill); } foreach (Skill skill in SkillList) { Console.WriteLine(skill.ToString());//需要重寫ToString方法 } Console.ReadKey(); }
結果如下:
方法2:
通過XmlElement與XmlAttributeCollection獲取節點名和結點下的屬性,XML檔案如下:
<SkillInfo> <SkillList> <Skill SkillID="20002" SkillEngName="Smash" TriggerType="1" ImageFile="data/gfx/image/gui_icon_skill_000.dds" AvailableRace="7" > <Name>重擊</Name> </Skill> <Skill SkillID="20003" SkillEngName="Hide" TriggerType="2" ImageFile="data/gfx/image/gui_icon_skill_001.dds" AvailableRace="1" > <Name>隱身</Name> </Skill> <Skill SkillID="20004" SkillEngName="Ikari" TriggerType="3" ImageFile="data/gfx/image/gui_icon_skill_002.dds" AvailableRace="1" > <Name>怒之翼</Name> </Skill> <Skill SkillID="20005" SkillEngName="Revenge" TriggerType="5" ImageFile="data/gfx/image/gui_icon_skill_003.dds" AvailableRace="2" > <Name>光之復仇</Name> </Skill> </SkillList> </SkillInfo>
主程式執行程式碼如下:
static void Main(string[] args) { List<Skill> skillList = new List<Skill>(); XmlDocument XmlSkillInfo = new XmlDocument(); XmlSkillInfo.Load(@".\..\..\xml技能資訊.txt"); XmlNode SkillListNode = XmlSkillInfo.FirstChild.FirstChild;//SkillList XmlNodeList SkillNodeList = SkillListNode.ChildNodes;//SkillList foreach (XmlNode skillNode in SkillNodeList)//Skill and Name { Skill skill = new Skill(); XmlElement ele = skillNode["Name"];//獲取名為"Name"的元素 skill.Name = ele.InnerText;//注意是InnerText不是Value XmlAttributeCollection xmlAttrCol = skillNode.Attributes;//獲取此節點下的所有屬性,並以集合的形式返回 skill.Id = Int32.Parse(xmlAttrCol["SkillID"].Value); skill.EngName = xmlAttrCol["SkillEngName"].Value; skill.TriggerYpye = Int32.Parse(xmlAttrCol["TriggerType"].Value); skill.ImageFile = xmlAttrCol["ImageFile"].Value; skill.AvailableRace = Int32.Parse(xmlAttrCol["AvailableRace"].Value); skillList.Add(skill); } foreach (Skill temp in skillList) { Console.WriteLine(temp.ToString()); } Console.ReadKey(); }
結果: