Simple XML operation-parsing skill XML
XML file
<skills> <skill> <id>1</id> <name lang="cn"> Big prison day means </name> <demage> -</demage> </skill> <skill> <id>2</id> <name lang="en"> Absolute Zero </name> <demage> $</demage> </skill> <skill> <id>3</id> <name lang="GER"> Dragon Soul </name> <demage> +</demage> </skill></skills>
Create the corresponding skill class, overriding the ToString method for easy output
classSkill { Public intID {Get;Set; } Public stringName {Get;Set; } Public stringLang {Get;Set; } Public intDemage {Get;Set; } Public Override stringToString () {return string. Format ("Id:{0},name:{1},lang:{2},demage:{3}", ID, Name, Lang, demage); } }
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Xml;7 8 namespaceXML Operations9 {Ten class Program One { A Static voidMain (string[] args) - { -List<skill> skilllist =NewList<skill>(); the -XmlDocument xmldoc =NewXmlDocument (); -Xmldoc.load ("SkillInfo.txt"); - + //root node -XmlNode root =Xmldoc.firstchild; + AXmlNodeList Skillsnodelist =Root. ChildNodes; at foreach(XmlNode Skillnodeinchskillsnodelist) - { -Skill Skill =NewSkill (); - foreach(XmlNode Filednodeinchskillnode.childnodes) - { - if(Filednode.name = ="ID") in { -Skill.id =int. Parse (filednode.innertext); to } + Else if(Filednode.name = ="name") - { theSkill. Name =Filednode.innertext; *Skill. Lang = filednode.attributes[0]. Value; $ }Panax Notoginseng Else if(Filednode.name = ="Demage") - { theSkill. Demage =int. Parse (filednode.innertext); + } A } the Skilllist.add (skill); + } - $ foreach(varIteminchskilllist) $ { - Console.WriteLine (item); - } the } - }Wuyi}
C#xml operation