我正在尝试比较web。配置文件从生产到非生产使用powershell脚本。我能够生成可以比较到一个级别的代码,但除此之外,我没有找到任何帮助在互联网上比较和更新xml文件与多层次深节点结构。下面是我的代码,它在文件中工作到第一级,但没有超越。是否有任何方法可以比较两个文件和更新,而无需硬编码任何节点元素?下面是我的代码,工作到一个级别。
$src_web_config = "C:Usersliveweb.config"
$dst_web_config = "C:Usersintweb.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)
# get top nodes
$src_top_node = $src_xml.DocumentElement.Name
$dst_top_node = $dst_xml.DocumentElement.Name
function add_node($add_node)
{
Write-Host "Node", $add_node.name, "doesn't exist"
$node = $dst_xml.importnode($add_node, $true)
$dst_xml.$dst_top_node.appendchild($node)
$dst_xml.Save($dst_web_config)
}
if ($src_top_node -eq $dst_top_node)
{
if ($src_xml.$src_top_node.HasChildNodes)
{
$src_xml.$src_top_node.ChildNodes | % {
if ($dst_xml.$dst_top_node.ChildNodes.name -contains $_.name)
{
if ($_.haschildnodes)
{
#nothing I can find
}
}
else
{
add_node($_)
}
}
}
}
问候,Vj
微软有一个XML Diff和补丁工具,你可以在。net应用程序中使用。微软还提供了一个用c#编写的示例GUI工具,它使用了这个库。您可以从这里下载GUI源代码安装程序。
我对使用Powershell中的XML Diff库没有任何特别的建议。在Powershell中使用。net库是另一个话题,但是你可以在网上找到相关信息。如果您不打算使用Powershell,也许您可以使用c#或VB。. NET,就像在GUI工具中一样。如果您不需要编写自己的脚本来自动化该过程,您可以根据需要使用他们的GUI工具来区分XML文件。