解析xml文件保存到List中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace testConsoleApplication1
{
    class test解析xml文件
    {
        public static void Main()
        {
            List<List<string>> info = getFileList();
            Console.WriteLine("一共有" + info.Count + "项");

            foreach (List<string> liststr in info)
            {
                foreach (string str in liststr)
                {
                    Console.WriteLine(str);
                }
            }

        }
        private static List<List<string>> getFileList()
        {
            string filename = @"D:\filesList.xml";
            List<List<string>> filelist = new List<List<string>>();
            List<string> tmpList = new List<string>();
            int i = -1;
            using (XmlReader reader = XmlReader.Create(filename)) //使用using不用显示释放资源
            {
                //XmlReader reader = XmlReader.Create(filename);
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        switch (reader.Name)
                        {
                            case "List":
                                Console.WriteLine("开始解析List");
                                break;
                            case "fileItem":
                                tmpList.Clear();
                                //Console.WriteLine("Start <fileItem> element.");
                                i++;
                                Console.WriteLine("第" + i + "个数组");
                                string attribute = reader["resid"];
                                tmpList.Add(attribute);
                                //if (attribute != null)
                                //{
                                //    Console.WriteLine("  Has attribute name: " + attribute);
                                //}
                                break;
                            case "atime":
                                if (reader.Read())
                                {
                                    tmpList.Add(reader.Value.Trim());
                                }
                                break;
                            case "ctime":
                                if (reader.Read())
                                {
                                    tmpList.Add(reader.Value.Trim());
                                }
                                break;
                            case "mtime":
                                if (reader.Read())
                                {
                                    tmpList.Add(reader.Value.Trim());
                                }
                                break;
                            case "name":
                                if (reader.Read())
                                {
                                    tmpList.Add(reader.Value.Trim());
                                }
                                break;
                            case "owner":
                                if (reader.Read())
                                {
                                    tmpList.Add(reader.Value.Trim());
                                }
                                break;
                            case "ownertype":
                                if (reader.Read())
                                {
                                    tmpList.Add(reader.Value.Trim());
                                }
                                break;
                            case "parent":
                                if (reader.Read())
                                {
                                    tmpList.Add(reader.Value.Trim());
                                }
                                break;
                            case "path":
                                if (reader.Read())
                                {
                                    tmpList.Add(reader.Value.Trim());
                                }
                                break;
                            case "quota":
                                if (reader.Read())
                                {
                                    tmpList.Add(reader.Value.Trim());
                                }
                                break;
                            case "size":
                                if (reader.Read())
                                {
                                    tmpList.Add(reader.Value.Trim());
                                }
                                break;
                            case "type":
                                if (reader.Read())
                                {
                                    tmpList.Add(reader.Value.Trim());
                                }
                                filelist.Add(tmpList);
                                foreach (string tmp in tmpList)
                                {
                                    Console.WriteLine(tmp);
                                }
                                break;
                        }

                    }
                }
            }
            return filelist;
        }

    }

}

有bug
posted @ 2012-08-08 20:25  gitran  阅读(496)  评论(0编辑  收藏  举报