2011-11-21 32 views
0
從Web.Config中讀取內部的ConfigurationSection

我試圖讀取Microsoft定義的ConfigurationSection:如何在ASP.NET

Microsoft.ApplicationServer.Caching.DataCacheClientSection

,但其類型內部,所以我不能以傳統的方式訪問它。

var dataCacheClientSection = (DataCacheClientSection)ConfigurationManager.GetSection("dataCacheClient"); 

我知道我可以使用反射,但沒有得到任何其他的選擇嗎?

回答

1

您可以用XmlDocument打開配置文件,然後使用XPath讀取您想要的任何內容。

喜歡的東西:

var xmlDoc = new XmlDocument(); 
xmlDoc.Load(Assembly.GetExecutingAssembly().Location + ".config"); 
XmlNode setting = xmlDoc.SelectSingleNode("configuration/..."); 

中的SelectSingleNode使用正確的XPath

+0

感謝。擔心這可能歸結爲此。 –