C#之读取XML

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);
            }

        }



    }
}

发布了70 篇原创文章 · 获赞 68 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/Rose_Girls/article/details/52044953