C#之读取XML

来源:互联网 发布:淘宝详情页私单价格 编辑:程序博客网 时间:2024/05/16 04:49
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Xml.Serialization;using System.Xml;namespace __杂集{    class ReadXml    {        public static void ReadXmlFile(string path)        {            //判断路径是否存在            //if (!Directory.Exists(path))            //    return;            //文件不存在            if (!File.Exists(path))                return;            //读取byte到内存流            using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path)))            {                //可使用此类在文档中加载、验证、编辑、添加和放置 XML                XmlDocument xdoc = new XmlDocument();                //从指定的流加载 XML 文档                xdoc.Load(ms);                //返回到第一项 也就是设置成从第一个节点开始读取                ms.Position = 0;                XmlSerializer xmlser = new XmlSerializer(typeof(List<XmlData>));                List<XmlData> list = (List<XmlData>)xmlser.Deserialize(ms);                foreach (var item in list)                {                    Console.WriteLine(item.name + "   " + item.id);                }                                Console.WriteLine("List count is:" + list.Count);            }        }    }}

0 0
原创粉丝点击