如何迭代字典<string,List <object >>

这个问题在这里已经有了答案:

  • 迭代字典的最佳方法是什么? 21个答案

  • 我写了一段时间的项目,目录结构输出到XML。 您可以轻松地为您修改此代码的目的:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using System.IO;
    
    namespace SAveDirectoriesXml
    {
        class Program
        {
            const string FILENAME = @"c:temptest.xml";
            const string FOLDER = @"c:temp";
            static XmlWriter writer = null;
            static void Main(string[] args)
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
    
                writer = XmlWriter.Create(FILENAME, settings);
                writer.WriteStartDocument(true);
    
                DirectoryInfo info = new DirectoryInfo(FOLDER);
                WriteTree(info);
    
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
                Console.WriteLine("Enter Return");
                Console.ReadLine();
    
            }
            static long WriteTree(DirectoryInfo info)
            {
                long size = 0;
                writer.WriteStartElement("Folder");
                try
                {
                    writer.WriteAttributeString("name", info.Name);
                    writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
                    writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
                    writer.WriteAttributeString("date", info.LastWriteTime.ToString());
    
    
                    foreach (DirectoryInfo childInfo in info.GetDirectories())
                    {
                        size += WriteTree(childInfo);
                    }
    
                }
                catch (Exception ex)
                {
                    string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
                    Console.WriteLine(errorMsg);
                    writer.WriteElementString("Error", errorMsg);
                }
    
                FileInfo[] fileInfo = null;
                try
                {
                    fileInfo = info.GetFiles();
                }
                catch (Exception ex)
                {
                    string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
                    Console.WriteLine(errorMsg);
                    writer.WriteElementString("Error",errorMsg);
                }
    
                if (fileInfo != null)
                {
                    foreach (FileInfo finfo in fileInfo)
                    {
                        try
                        {
                            writer.WriteStartElement("File");
                            writer.WriteAttributeString("name", finfo.Name);
                            writer.WriteAttributeString("size", finfo.Length.ToString());
                            writer.WriteAttributeString("date", info.LastWriteTime.ToString());
                            writer.WriteEndElement();
                            size += finfo.Length;
                        }
                        catch (Exception ex)
                        {
                            string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
                            Console.WriteLine(errorMsg);
                            writer.WriteElementString("Error", errorMsg);
                        }
                    }
                }
    
                writer.WriteElementString("size", size.ToString());
                writer.WriteEndElement();
                return size;
    
            }
        }
    }
    
    链接地址: http://www.djcxy.com/p/30385.html

    上一篇: how to iterate though dictionary<string,List<object>>

    下一篇: Casting ScriptBlock as Action[T] for Generic.List[T] ForEach method