我正在阅读 IIS 的 applicationHost.xml.config 文件。我在站点中获取每个站点的虚拟目录,然后从那时获取我需要的信息。物理路径和路径等信息。然后我需要获取绑定。
当我有两个应用程序节点时,我无法弄清楚如何有效地到达“绑定”元素节点。(下面显示了一个示例)但是,如果有一个应用程序节点,我可以使用“site.ParentNode.NextSibling.ChildNodes”来获取相应绑定的列表。
在此先感谢您的帮助!
我的代码:
XDocument.Load(@"C:\\windows\\system32\\inetsrv\\config\\applicationHost.config");
XmlNodeList siteList = XDocument.SelectNodes("/configuration/system.applicationHost/sites/site/application/virtualDirectory");
foreach (XmlNode site in siteList)
{
XmlAttribute XmlAttributeParentParentName = (XmlAttribute)site.ParentNode.ParentNode.Attributes.GetNamedItem("name");
XmlAttribute XmlAttributePath = (XmlAttribute)site.Attributes.GetNamedItem("path");
XmlAttribute XmlAttributePhysicalPath = (XmlAttribute)site.Attributes.GetNamedItem("physicalPath");
XmlNodeList BindingList = (XmlNodeList)site.ParentNode.NextSibling.ChildNodes;
string path = XmlAttributePath.Value.ToString();
string siteName = XmlAttributeParentParentName.Value.ToString();
string physicalPath = XmlAttributePhysicalPath.Value.ToString();
string firstBindingElement = BindingList[0].Attributes.GetNamedItem("bindingInformation").Value.ToString();
//do something with the variables.
//rest of code is here
}
以下是站点节点的示例:
<site name="Site Name" id="20" serverAutoStart="true">
<application path="/" applicationPool="SiteAppPool">
<virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot\SiteName" />
</application>
<application path="/store" applicationPool="SiteAppPool">
<virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot\SiteName\store" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:80:sitename.com" />
</bindings>
</site>