2012-07-12 122 views
1

我正在阅读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> 

回答

0

我希望我没有在这里失去了一些东西,但不能使用在迭代的XmlNode上选择节点,如下所示:

XmlNodeList BindingList = (XmlNodeList)site.SelectNodes("../../bindings/binding"); 

这应该从您的迭代节点(它是virtualDirectory节点)的根到它的父节点(它是一个应用程序节点)到它的父节点(它是节点节点),直到绑定节点,最后到一个列表绑定节点在这个?