关闭

一个读XML的练习

标签: xmlstringexceptionencodingclassnull
428人阅读 评论(0) 收藏 举报
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace ReadXml
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> DicXmlPairs = new MyXMLReader().DicXmlPairs;

            foreach (KeyValuePair<string,string> item in DicXmlPairs)
            {
                Console.WriteLine(item.Key);
                Console.WriteLine(item.Value);
            }

            Console.ReadKey();
        }
        public class MyXMLReader
        {
            public Dictionary<string, string> DicXmlPairs 
            {
                get 
                {
                    return this.GetAllXmlContent();
                } 
            }
            /// <summary>
            /// 获取一个Xml文件的 key value
            /// </summary>
            /// <param name="xmlFile"></param>
            /// <param name="dicXmlContents"></param>
            private  void AnalyzeXml(FileInfo xmlFile, ref Dictionary<string, string> dicXmlContents)
            {
                XmlTextReader reader = null;
                try
                {
                    reader = new XmlTextReader(xmlFile.FullName);
                    reader.WhitespaceHandling = WhitespaceHandling.None;
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            if ("add".Equals(reader.Name) && reader.HasAttributes && 2 == reader.AttributeCount)
                            {
                                dicXmlContents.Add(reader.GetAttribute("key"), reader.GetAttribute("value"));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }                
            }
            /// <summary>
            /// 获得所有XML文件中的key value
            /// </summary>
            private  Dictionary<string,string> GetAllXmlContent()
            {
                Dictionary<string, string> dicXmlContents = new Dictionary<string, string>();
                string xmlPath = AppDomain.CurrentDomain.BaseDirectory;
                List<FileInfo> lstFiles = new List<FileInfo>();

                GetXMLFiles(new DirectoryInfo(xmlPath), ref lstFiles);

                foreach (FileInfo xmlfile in lstFiles)
                {
                    AnalyzeXml(xmlfile, ref dicXmlContents);
                }
                return  dicXmlContents;
            }
            //获取全部的Xml文件
            private  void GetXMLFiles(DirectoryInfo xmlDir, ref List<FileInfo> lstFiles)
            {
                foreach (FileInfo files in xmlDir.GetFiles())
                {
                    if (files.Extension.ToUpper().EndsWith(".XML"))
                    {
                        lstFiles.Add(files);
                    }
                }
                foreach (DirectoryInfo subDir in xmlDir.GetDirectories())
                {
                    GetXMLFiles(subDir, ref lstFiles);
                }
            }
        }

    }
}

xml文件内容

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="NewKey0" value="Monday, January 23, 2006 2:56:14 PM" />
    <add key="NewKey1" value="Monday, January 23, 2006 3:15:18 PM" />
    <add key="NewKey2" value="Monday, January 23, 2006 3:16:29 PM" />
  </appSettings>
</configuration>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:25813次
    • 积分:364
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:1篇
    • 译文:0篇
    • 评论:6条
    最新评论