我有一个XML文件,需要运行时节点下的条目(以红色突出显示):
我可以通过以下PowerShell脚本实现此目的
$appFrameContainerAppConfig = 'D:\H.Infrastructure.AppFrame.Container.exe.config'
$doc = (Get-Content $appFrameContainerAppConfig) -as [Xml]
$root = $doc.get_DocumentElement()
$newDependentAssembly = $doc.CreateElement("dependentAssembly")
$newAssemblyIdentity = $doc.CreateElement("assemblyIdentity")
$newAssemblyIdentity.SetAttribute("name","EntityFramework")
$newAssemblyIdentity.SetAttribute("publicKeyToken","b77a5c561934e089")
$newAssemblyIdentity.SetAttribute("culture","neutral")
$newDependentAssembly.AppendChild($newAssemblyIdentity)
$newCodeBase = $doc.CreateElement("codeBase")
$newCodeBase.SetAttribute("version","6.0.0.0")
$newCodeBase.SetAttribute("href","EntityFramework_6.1.3/EntityFramework.dll")
$newDependentAssembly.AppendChild($newCodeBase)
$root.runtime.assemblyBinding.AppendChild($newDependentAssembly)
$newDependentAssembly = $doc.CreateElement("dependentAssembly")
$newAssemblyIdentity = $doc.CreateElement("assemblyIdentity")
$newAssemblyIdentity.SetAttribute("name","EntityFramework.SqlServer")
$newAssemblyIdentity.SetAttribute("publicKeyToken","b77a5c561934e089")
$newAssemblyIdentity.SetAttribute("culture","neutral")
$newDependentAssembly.AppendChild($newAssemblyIdentity)
$newCodeBase = $doc.CreateElement("codeBase")
$newCodeBase.SetAttribute("version","6.0.0.0")
$newCodeBase.SetAttribute("href","EntityFramework_6.1.3/EntityFramework.SqlServer.dll")
$newDependentAssembly.AppendChild($newCodeBase)
$root.runtime.assemblyBinding.AppendChild($newDependentAssembly)
$newDependentAssembly = $doc.CreateElement("dependentAssembly")
$newAssemblyIdentity = $doc.CreateElement("assemblyIdentity")
$newAssemblyIdentity.SetAttribute("name","System.Data.SQLite")
$newAssemblyIdentity.SetAttribute("publicKeyToken","db937bc2d44ff139")
$newAssemblyIdentity.SetAttribute("culture","neutral")
$newDependentAssembly.AppendChild($newAssemblyIdentity)
$newCodeBase = $doc.CreateElement("codeBase")
$newCodeBase.SetAttribute("version","1.0.98.0")
$newCodeBase.SetAttribute("href","SQLite_1.0.98.1/System.Data.SQLite.dll")
$newDependentAssembly.AppendChild($newCodeBase)
$root.runtime.assemblyBinding.AppendChild($newDependentAssembly)
$newDependentAssembly = $doc.CreateElement("dependentAssembly")
$newAssemblyIdentity = $doc.CreateElement("assemblyIdentity")
$newAssemblyIdentity.SetAttribute("name","System.Data.SQLite.EF6")
$newAssemblyIdentity.SetAttribute("publicKeyToken","db937bc2d44ff139")
$newAssemblyIdentity.SetAttribute("culture","neutral")
$newDependentAssembly.AppendChild($newAssemblyIdentity)
$newCodeBase = $doc.CreateElement("codeBase")
$newCodeBase.SetAttribute("version","1.0.98.0")
$newCodeBase.SetAttribute("href","SQLite_1.0.98.1/System.Data.SQLite.EF6.dll")
$newDependentAssembly.AppendChild($newCodeBase)
$root.runtime.assemblyBinding.AppendChild($newDependentAssembly)
$newDependentAssembly = $doc.CreateElement("dependentAssembly")
$newAssemblyIdentity = $doc.CreateElement("assemblyIdentity")
$newAssemblyIdentity.SetAttribute("name","System.Data.SQLite.Linq")
$newAssemblyIdentity.SetAttribute("publicKeyToken","db937bc2d44ff139")
$newAssemblyIdentity.SetAttribute("culture","neutral")
$newDependentAssembly.AppendChild($newAssemblyIdentity)
$newCodeBase = $doc.CreateElement("codeBase")
$newCodeBase.SetAttribute("version","1.0.98.0")
$newCodeBase.SetAttribute("href","SQLite_1.0.98.1/System.Data.SQLite.Linq.dll")
$newDependentAssembly.AppendChild($newCodeBase)
$root.runtime.assemblyBinding.AppendChild($newDependentAssembly)
Write-Output $root.runtime.assemblyBinding.dependentAssembly
$doc.Save($appFrameContainerAppConfig)
我需要一个条件来评估XML中是否存在以红色突出显示的条目,如果是这样,请避免添加相同的条件。
if($root.SelectNodes('//configuration/runtime/assemblyBinding/dependentAssembly/assemblyIdentity[@name="EntityFramework"]')) {
} else {
}
我在这里使用的条件似乎不起作用。它不会进入,如果或其他条件。有什么建议吗?
答案 0 :(得分:0)
检查<runtime>
节点是否已包含条目,例如像这样:
if ($root.SelectNodes('//runtime/dependentAssembly').Count -eq 0) {
...
}
如果您需要更细致的检查,请调整XPath expression。
编辑:显然您的XML数据使用名称空间,因此您需要一个名称空间管理器来选择具有名称空间的节点:
$nsm = New-Object System.Xml.XmlNamespaceManager($doc.NameTable)
$nsm.AddNamespace('ns', 'urn:schemas-microsoft-com:asm.v1')
if ($doc.SelectNodes('//ns:assemblyBinding/dependentAssembly/assemblyIdentity[@name="EntityFramework"]', $nsm).Count -eq 0) {
...
}