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
  • 相关阅读:
    初识Redis
    一次kafka的offset回退事件及相关知识点
    接口透传
    看懂Oracle执行计划
    Oracle中merge into的使用
    动态规划算法:0/1背包问题 (0/1 Knapsack Problem)
    动态规划算法:硬币找零(Minimum Coin Change)
    链表:按照左右半区的方式重新组合单链表
    链表:删除链表中倒数第K个节点
    链表:合并两个有序链表
  • 原文地址:https://www.cnblogs.com/gitran/p/3644144.html
Copyright © 2011-2022 走看看