C#解析xml/json/excel

来源:互联网 发布:域名投资人联系 编辑:程序博客网 时间:2024/06/05 08:24

C#解析xml/json/excel

1、C#解析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>

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _xml {    class Skill {        public  int Id { get; set; }        public string Name { get; set; }        public string EngName { get; set; }        public int TriggerType { get; set; }        public string ImageFile { get; set; }        public int AvailableRace { get; set; }        public override string ToString()        {            return string.Format("Id: {0}, Name: {1}, EngName: {2}, TriggerType: {3}, ImageFile: {4}, AvailableRace: {5}", Id, Name, EngName, TriggerType, ImageFile, AvailableRace);        }    }}

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<Skill> skillList = new List<Skill>();            XmlDocument xmlDoc = new XmlDocument();            xmlDoc.Load("xml技能信息.txt");            //XmlNode skillInfoNode = xmlDoc.FirstChild;            //XmlNode skillListNode = skillInfoNode.FirstChild;            XmlNode skillListNode = xmlDoc.FirstChild.FirstChild;            XmlNodeList skillNodeList = skillListNode.ChildNodes;            foreach (XmlNode skillNode in skillNodeList)            {                Skill skill = new Skill();                XmlElement ele = skillNode["Name"];                skill.Name = ele.InnerText;                XmlAttributeCollection col = skillNode.Attributes;//获取该结点属性的集合                //skill.Id = Int32.Parse(col["SkillID"].Value);                XmlAttribute idAttribute = col["SkillID"];//通过字符串索引器 获取一个属性对象                skill.Id = Int32.Parse(idAttribute.Value);                skill.EngName = col["SkillEngName"].Value;                skill.TriggerType = Int32.Parse(col["TriggerType"].Value);                skill.ImageFile = col["ImageFile"].Value;                skill.AvailableRace = Int32.Parse(col["AvailableRace"].Value);                skillList.Add(skill);            }            foreach (Skill s in skillList)            {                Console.WriteLine(s);            }            Console.ReadKey();        }    }}


2、C#解析json文件

[{"id":2,"name":"天下无双","damage":123 },{"id":3,"name":"天下无贼","damage":21 },{"id":4,"name":"咫尺天涯","damage":900 }]

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _json {    class Skill    {        public int id;        public int damage;        public string name;        public override string ToString()        {            return string.Format("Id: {0}, Damage: {1}, Name: {2}", id, damage, name);        }    }}

{"Name":"tom","Level":99,"Age":18,"SkillList":[{"id":2,"name":"天下无双","damage":123 },{"id":3,"name":"天下无贼","damage":21 },{"id":4,"name":"咫尺天涯","damage":900 }]}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _json {    class Player    {    //    public string name;//字段或者属性名 要跟json里面的对应    //    public int level;        public string Name { get; set; }        public int Level { get; set; }        public int Age { get; set; }        public List<Skill> SkillList { get; set; }        public override string ToString()        {            return string.Format("Name: {0}, Level: {1}, Age: {2}, SkillList: {3}", Name, Level, Age, SkillList);        }    }}

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using LitJson;namespace _json {    class Program {        static void Main(string[] args) {            //使用litjson进行解析json文本            //两种引入litjson的方法            //1,去litjson的网站下载litjson.dll 然后添加引用 找到dll所在目录            //2,右键引用 打开管理netget程序包,在联机里面搜索litjson  在搜索结果中选择一个 点击安装            //List<Skill> skillList = new List<Skill>();            //我们使用jsonMapper去解析json文本            //jsondata代表一个数组或者一个对象            //在这里jsonData代表数组             //JsonData jsonData = JsonMapper.ToObject(File.ReadAllText("json技能信息.txt"));            //foreach (JsonData temp in jsonData)//在这里temp代表一个对象            //{            //    Skill skill = new Skill();            //    JsonData idValue =temp["id"]; //通过字符串索引器可以取得键值对的值            //    JsonData nameValue = temp["name"];            //    JsonData damageValue = temp["damage"];            //    int id = Int32.Parse(idValue.ToString());            //    int damage = Int32.Parse(damageValue.ToString());            //    //Console.WriteLine(id+":"+nameValue.ToString()+":"+damage);            //    skill.id = id;            //    skill.damage = damage;            //    skill.name = nameValue.ToString();            //    skillList.Add(skill);            //}            //foreach (var temp in skillList)            //{            //    Console.WriteLine(temp);            //}            //使用泛型去解析json            //json里面对象的键必须跟定义的类里面的字段或者属性保持一致            //Skill[] skillArray= JsonMapper.ToObject<Skill[]>(File.ReadAllText("json技能信息.txt"));            //foreach (var temp in skillArray)            //{            //    Console.WriteLine(temp);            //}            //List<Skill> skillList = JsonMapper.ToObject<List<Skill>>(File.ReadAllText("json技能信息.txt"));            //foreach (var temp in skillList) {            //    Console.WriteLine(temp);            //}            //Player p= JsonMapper.ToObject<Player>(File.ReadAllText("主角信息.txt"));            //Console.WriteLine(p);            //foreach (var temp in p.SkillList)            //{            //    Console.WriteLine(temp);            //}            Player p = new Player();            p.Name = "花千骨";            p.Level = 100;            p.Age = 16;            string json =JsonMapper.ToJson(p);            Console.WriteLine(json);            Console.ReadKey();        }    }}


3、C#解析excel文件

using System;using System.Collections.Generic;using System.Data;using System.Data.OleDb;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _Excel {    class Program {        static void Main(string[] args)        {            string fileName = "装备信息.xls";            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";            //创建连接到数据源的对象            OleDbConnection connection = new OleDbConnection(connectionString);            //打开连接            connection.Open();                        string sql = "select * from [Sheet1$]";//这个是一个查询命令                        OleDbDataAdapter adapter = new OleDbDataAdapter(sql,connection);            DataSet dataSet = new DataSet();//用来存放数据 用来存放DataTable            adapter.Fill(dataSet);//表示把查询的结果(datatable)放到(填充)dataset里面                        connection.Close();//释放连接资源            //取得数据            DataTableCollection tableCollection= dataSet.Tables;//获取当前集合中所有的表格                        DataTable table = tableCollection[0];//因为我们只往dataset里面放置了一张表格,所以这里取得索引为0的表格就是我们刚刚查询到的表格            //取得表格中的数据            //取得table中所有的行            DataRowCollection rowCollection = table.Rows;//返回了一个行的集合            //遍历行的集合,取得每一个行的datarow对象            foreach (DataRow row in rowCollection)            {                //取得row中前8列的数据 索引0-7                for (int i = 0; i < 8; i++)                {                    Console.Write(row[i]+" ");                }                Console.WriteLine();                            }            Console.ReadKey();        }    }}



====================================================================================
结束。

0 0
原创粉丝点击