1

web.configファイルがあり、特定のキー名の接続文字列値を取得したい。

<connectionStrings>
        <add name="abc" connectionString="Server=(local);Initial Catalog=abc;Integrated Security=SSPI;Max Pool Size=25" providerName="System.Data.SqlClient" />
        <add name="cde" connectionString="Server=(local);Initial Catalog=cde; Integrated Security=SSPI;Max Pool Size=50" providerName="System.Data.SqlClient" />
    </connectionStrings>

configurationManagerで接続文字列を取得できることはわかっていますが、XMLリーダーで取得したいと思います。現在使用しています

XDocument document = XDocument.Load(fullPath);
var connectionString = from c in document.Descendants(connectionStrings)
select c ;

両方の接続文字列を取得しています。しかし、特定の「abc」接続文字列を取得したい。手伝ってくれませんか。

4

3 に答える 3

2
XDocument document = XDocument.Load(fullPath);
var connectionString = from c in document.Descendants("connectionStrings").Descendants("add")
    where c.Attribute("name").Value == "abc"                
    select c;
于 2013-03-08T06:47:45.320 に答える
1

代替 (少し流暢な構文を使用)

var connectionString = document.Descendants("connectionStrings")
                       .Descendants("add")
                       .First(x => x.Attribute("name").Value == "abc").Attribute("connectionString").Value;
于 2013-03-08T07:07:42.200 に答える
0

これを試して

XDocument document = XDocument.Load(fullPath);
var connectionString = from c in document.Descendants("connectionStrings")
                       where c.name=="abc"               
                       select c ;
于 2013-03-08T06:41:57.043 に答える