如何使用XMLNode.SelectSingleNode

时间:2015-02-19 18:19:11

标签: c# xml

我有以下XML:

<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="VirtualPrintFeesGPAddin.Properties.Settings.GPConnectionString"
            connectionString="Data Source=CONSULTING118\SVR2008A;Initial Catalog=TWO;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

如何使用xmlnode.SelectSingleNode检索connectionString = value?

1 个答案:

答案 0 :(得分:0)

假设你在xmldocument对象中有xml,那就是:

XmlDocument doc = new XmlDocument();
doc.LoadXML("<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name=\"VirtualPrintFeesGPAddin.Properties.Settings.GPConnectionString\"
            connectionString=\"Data Source=CONSULTING118\SVR2008A;Initial Catalog=TWO;Integrated Security=True\"
            providerName=\"System.Data.SqlClient\" />
    </connectionStrings>
</configuration>");

String str = doc.SelectSingleNode("//configuration/connectionStrings/add[@name='VirtualPrintFeesGPAddin.Properties.Settings.GPConnectionString']").Attributes["connectionString"].Value;

如果你想获得更多那个属性:

XmlNode n = doc.SelectSingleNode("//configuration/connectionStrings/add[@name='VirtualPrintFeesGPAddin.Properties.Settings.GPConnectionString']");

String str = n.Attributes["connectionString"].Value;