C#xml operation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.