【发布时间】:2015-03-25 13:07:36
【问题描述】:
我试图在 XML 文件中显示每个子节点,但有些节点没有显示。下面是我的 powershell 脚本。
$dst_web_config = "C:\Users\vel\web.config"
$src_web_config = "f:\Script_test\web.config"
# Creating XML objects for live and integration
$src_xml = New-Object -TypeName XML
$dst_xml = New-Object -TypeName XML
$src_xml.Load($src_web_config)
$dst_xml.Load($dst_web_config)
# Check nodes available
$src_top_node = $src_xml.DocumentElement.Name
$dst_top_node = $dst_xml.DocumentElement.Name
# Defining recursive function
function compare_nodes($node)
{
if ($dst_xml.$dst_top_node.$node.HasChildNodes)
{
$dst_xml.SelectNodes("$dst_top_node/$node/*") | Select-Object -Expand Name | % {
$xpath_node = [string]$node + "/" + [string]$_
$path_node = [string]$node + "." + [string]$_
write-host $path_node
compare_nodes($path_node)
}
}
else
{
#write-host "No Child found for" $node
return
}
}
if ($src_top_node -eq $dst_top_node)
{
if ($dst_xml.$dst_top_node.HasChildNodes)
{
#write-host "Child Node exist for " $dst_top_node
$dst_xml.SelectNodes("$dst_top_node/*") | Select-Object -Expand Name | % {
compare_nodes($_)
}
}
else
{
write-host "No child nodes for "$dst_top_node
}
}
else
{
write-host $src_top_node" is not present in destination file"
}
我将 XML 文件的某些部分放置在未显示所有子节点的位置。
<configuration>
<configSections>
<section name="siteMapConfiguration" type="NRI.Helpers.Configuration.SiteMapConfiguration, NRI.Helpers" />
<section name="promotionCategoriesSection" type="NRI.Helpers.Configuration.PromotionCategoriesConfigurationSection, NRI.Helpers" />
<section name="BaseRestExtensions" type="domino.Web.BaseRest.Configuration.BaseRestSection, domino" requirePermission="false" />
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
<section name="townSearchSection" type="NRI.Helpers.Configuration.SearchConfigurationSection, NRI.Helpers" />
<section name="userTypesSection" type="NRI.Helpers.Configuration.UserTypesConfigurationSection, NRI.Helpers" />
</configSections>
<system.net>
<mailSettings>
<smtp>
<network host="127.0.0.1" userName="username" password="password" />
</smtp>
</mailSettings>
</system.net>
在脚本输出中,我没有从上面的 xml 中获得“sectionGroup”、“smtp”。这是两个典型的例子。如何确保脚本显示所有子节点?
【问题讨论】:
标签: xml powershell