一个读XML的练习

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值