使用 Linq To XML 从 applicationHost.config 文件中检索数据
Retrieving data from applicationHost.config file using Linq To XML
我正在尝试从 applicationHost.config 文件中获取以下 'dontLog' 属性的值:
<location path="Default Web Site">
<system.webServer>
<security>
<authentication>
<windowsAuthentication enabled="true" />
</authentication>
</security>
<httpLogging dontLog="false" />
</system.webServer>
</location>
我能够读取所有 'location' 节点,但是到 XML 的 LinQ 可能会非常混乱。
这是我的代码:
IEnumerable<XElement> locationNodes = doc.Document.Descendants().Where(x => x.Name.LocalName == "location").ToList();
foreach (XElement e in locationNodes)
{
Location location = new Location()
{
Name = e.Attribute("path").Value
};
if (location.Name == "Default Web Site")
{
//not working
IEnumerable<XElement> httpLogging = e.Elements().Where(x => x.Name.LocalName == "httpLogging").ToList();
//need to obtain the value of 'dontLog' attribute and return null if not exist
}
}
public class Location
{
public string Name { get; set; }
}
谢谢
最大值
在 VB.Net 中是
Dim location As XElement
'example given
location = <location path="Default Web Site">
<system.webServer>
<security>
<authentication>
<windowsAuthentication enabled="true"/>
</authentication>
</security>
<httpLogging dontLog="false"/>
</system.webServer>
</location>
Dim dontLogValue As String = location.<system.webServer>.<httpLogging>.@dontLog
你的问题没有给出任何迹象表明程序运行时会发生什么(除了它不工作),但很可能(基于你的代码)你没有得到任何回报,这是因为 XML 的结构以及 .Elements()
的工作原理。 .Elements()
returns "Returns a filtered collection of the child elements of this element or document".
<location>
的唯一子元素是 <system.webServer>
- 它永远不会下降到 <httpLogging>
,它是 <system.webServer>
的子元素。
与大多数事情一样,有多种方法可以完成您想要做的事情。这是一个。
if (location.Name == "Default Web Site")
{
string dontLog = (string)e.Descendants("httpLogging").Attributes("dontLog").FirstOrDefault();
}
请注意上面代码中的几件事。首先,调用 .Descendants("httpLogging")
。与 .Elements()
不同,.Descendants()
将 return 当前元素的所有 descendant 节点,并且通过传入 "httpLogging") 它将 return 所有具有该名称的后代节点。
其次,如果属性不存在,显式转换 (string)
将 return null(否则它将 return 属性的字符串值)。
最后,根据示例结构,每组 <location>
将只有一个 <httpLogging>
元素,因此调用 FirstOrDefault()
将 return 第一次出现,或默认值(对于引用类型为 null),如果没有的话。
我正在尝试从 applicationHost.config 文件中获取以下 'dontLog' 属性的值:
<location path="Default Web Site">
<system.webServer>
<security>
<authentication>
<windowsAuthentication enabled="true" />
</authentication>
</security>
<httpLogging dontLog="false" />
</system.webServer>
</location>
我能够读取所有 'location' 节点,但是到 XML 的 LinQ 可能会非常混乱。 这是我的代码:
IEnumerable<XElement> locationNodes = doc.Document.Descendants().Where(x => x.Name.LocalName == "location").ToList();
foreach (XElement e in locationNodes)
{
Location location = new Location()
{
Name = e.Attribute("path").Value
};
if (location.Name == "Default Web Site")
{
//not working
IEnumerable<XElement> httpLogging = e.Elements().Where(x => x.Name.LocalName == "httpLogging").ToList();
//need to obtain the value of 'dontLog' attribute and return null if not exist
}
}
public class Location
{
public string Name { get; set; }
}
谢谢 最大值
在 VB.Net 中是
Dim location As XElement
'example given
location = <location path="Default Web Site">
<system.webServer>
<security>
<authentication>
<windowsAuthentication enabled="true"/>
</authentication>
</security>
<httpLogging dontLog="false"/>
</system.webServer>
</location>
Dim dontLogValue As String = location.<system.webServer>.<httpLogging>.@dontLog
你的问题没有给出任何迹象表明程序运行时会发生什么(除了它不工作),但很可能(基于你的代码)你没有得到任何回报,这是因为 XML 的结构以及 .Elements()
的工作原理。 .Elements()
returns "Returns a filtered collection of the child elements of this element or document".
<location>
的唯一子元素是 <system.webServer>
- 它永远不会下降到 <httpLogging>
,它是 <system.webServer>
的子元素。
与大多数事情一样,有多种方法可以完成您想要做的事情。这是一个。
if (location.Name == "Default Web Site")
{
string dontLog = (string)e.Descendants("httpLogging").Attributes("dontLog").FirstOrDefault();
}
请注意上面代码中的几件事。首先,调用 .Descendants("httpLogging")
。与 .Elements()
不同,.Descendants()
将 return 当前元素的所有 descendant 节点,并且通过传入 "httpLogging") 它将 return 所有具有该名称的后代节点。
其次,如果属性不存在,显式转换 (string)
将 return null(否则它将 return 属性的字符串值)。
最后,根据示例结构,每组 <location>
将只有一个 <httpLogging>
元素,因此调用 FirstOrDefault()
将 return 第一次出现,或默认值(对于引用类型为 null),如果没有的话。