zoukankan      html  css  js  c++  java
  • 解析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
  • 相关阅读:
    Elementary Methods in Number Theory Exercise 1.2.25
    Elementary Methods in Number Theory Exercise 1.2.14
    图解欧几里德算法
    图解欧几里德算法
    Elementary Methods in Number Theory Exercise 1.2.14
    Android中的长度单位详解(dp、sp、px、in、pt、mm)
    分享下多年积累的对JAVA程序员成长之路的总结
    android异常之都是deamon惹的祸The connection to adb is down, and a severe error has occured.
    TomatoCartv1.1.8.2部署时报错
    JavaScript浏览器对象之二Document对象
  • 原文地址:https://www.cnblogs.com/gitran/p/3644144.html
Copyright © 2011-2022 走看看