C#读取XML 练习2

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;

            string sql=string.Empty;
            if (DicXmlPairs.TryGetValue("id1", out sql))
            {
                Console.WriteLine(sql);
            }

            Console.ReadKey();
        }
        public class MyXMLReader
        {
            public Dictionary<string, string> DicXmlPairs 
            {
                get 
                {
                    return this.GetAllXmlContent();
                } 
            }
            private string key = string.Empty;
            /// <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())
                    {
                       switch (reader.NodeType) 
                        {
                            case XmlNodeType.Element:
                                {
                                    if ("sql".Equals(reader.Name) && reader.HasAttributes && 1 == reader.AttributeCount)
                                    {
                                        key = reader.GetAttribute("name");
                                    }
                                }
                                break;
                            case XmlNodeType.CDATA: 
                                {
                                    dicXmlContents.Add(key, reader.ReadContentAsString());
                                }
                                break;
                           default:
                                break;
                        }
                    }
                }
                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" ?>
<sql-definations>


  <!--lalalaalala-->
  <sql name="id1">
    <![CDATA[
      select xxx from xxx where xxxx=xx
    ]]>
  </sql>


</sql-definations>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值