C# linq到xml从web.config获取属性值

C# linq到xml从web.config获取属性值,c#,asp.net,xml,linq,C#,Asp.net,Xml,Linq,你好,我有以下问题。我想使用linq从以下代码中获取baseLocationD:\NewSites\TEST的值。 我试过一些方法,但似乎不起作用。 你知道怎么做吗? 提前谢谢。 托拉斯 我开始时是这样的,但是这个返回空值 XDocument document = XDocument.Load("C:\\web.config"); var dataList = from item in document.Descendants("configuration") select item; 这是我

你好,我有以下问题。我想使用linq从以下代码中获取baseLocationD:\NewSites\TEST的值。 我试过一些方法,但似乎不起作用。 你知道怎么做吗? 提前谢谢。 托拉斯

我开始时是这样的,但是这个返回空值

XDocument document = XDocument.Load("C:\\web.config");
var dataList = from item in document.Descendants("configuration") select item;
这是我的XML

<?xml version="1.0"?>
<configuration>
    <configSections>
    </configSections>
    <log4net>
    </log4net>
    <web>
        <website runtimeMode="Development" siteName="TEST" baseLocation="D:\NewSites\TEST"      sitePath="D:\NewSites\TEST\WebApps\Website" siteUri="http://test.co.uk" s    iteEmail="test@gmail.com" />
        <cms defaultTemplate="TEST\Content.aspx" templatesUrl="/Manager/Templates/">
        <publishingLocations>
            <publishingLocation name="TEST" uri="http://test.co.uk" path="WebApps\Website" />
        </publishingLocations>
        <redirectables />
        <searchEngineSiteMapNotifications />
        <siteMapXmlUrls />
        <pingServices />
        <reservedTemplates />
        <templateFilters />
        </cms>
    </web>
    <location path="Manager">
    </location>
    <connectionStrings>
    </connectionStrings>
    <system.web>
    </system.web>
</configuration>

要使用LINQ和C来提取属性,请使用如下内容

XDocument document = Xdocument.Load("~/web.config");
var location = document.Descendants().Single(i=>i.Attribute("baseLocation") !=null)
               .Attribute("baseLocation").Value;

要使用LINQ和C来提取属性,请使用如下内容

XDocument document = Xdocument.Load("~/web.config");
var location = document.Descendants().Single(i=>i.Attribute("baseLocation") !=null)
               .Attribute("baseLocation").Value;
如果您需要进一步的帮助,或者如果有帮助,请联系我

如果您需要进一步的帮助,请告诉我,或者它是否有帮助,请标记。

这个怎么样:

string baseLocation = document.Descendants("website").First().Attribute("baseLocation").Value;
这个怎么样:

string baseLocation = document.Descendants("website").First().Attribute("baseLocation").Value;

LINQ到SQL和XML应该如何结合在一起?请展示您的方法,以便我们可以为您修复它们。是的,我很抱歉这是XML的大错误。LINQ到SQL和XML应该如何结合在一起?请展示您的方法,这样我们可以为您修复它们。是的,我很抱歉这是XML的大类型错误。