[英]Need to edit the XML file using Powershell
我有XML内容,我需要从节点的特定元素中删除整行。
XML档案:
<Host name="login.net">
<Path name="Block">
<Path name="webservices/login" authType="qwerty" requireSession="true" />
<Path name="login" authType="qwerty" requireSession="true" />
<Path name="weblogin" requireSession="true" authType="shibboleth" />
</Path>
<Path name="retailpdf" applicationId="retailpdf" authType="qwerty" requireSession="true" requireSessionWith="retailpdf" />
<Path name="managemyaccount" applicationId="managemyaccount" authType="qwerty" requireSession="true" requireSessionWith="managemyaccount" />
<Path name="approve" applicationId="approve" authType="qwerty" requireSession="true" requireSessionWith="approve" />
<Path name="latesignin" applicationId="latesignin" authType="qwerty" requireSession="true" requireSessionWith="latesignin" />
<Path name="fblogin" applicationId="fblogin" authType="qwerty" requireSession="true" requireSessionWith="fblogin" />
</Host>
</RequestMap>
#需要删除id零售的完整节点
试图通过Powershell脚本删除行
$XMLfilepath = 'C:\Test\MYfile.xml'
psedit $XMLfilepath
$Pathxml = [xml] (Get-Content $XMLfilepath)
$nodes = $xml.SelectNodes("/RequestMapper/Path");
{Remove-Item "<Path name="fblogin" applicationId="fblogin" authType="shibboleth" requireSession="true" requireSessionWith="fblogin" />"}
$XMLfilepathNew = 'C:\Test\Myfiledone.xml'
$Pathxml.Save($XMLfilepathNew)
psedit $XMLfilepathNew
需要删除XML内容中提到的行和节点,如下所示:
预期产量:
<Host name="login.net">
<Path name="Block">
<Path name="webservices/login" authType="qwerty" requireSession="true" />
<Path name="login" authType="qwerty" requireSession="true" />
<Path name="weblogin" requireSession="true" authType="shibboleth" />
</Path>
<Path name="managemyaccount" applicationId="managemyaccount" authType="qwerty" requireSession="true" requireSessionWith="managemyaccount" />
<Path name="approve" applicationId="approve" authType="qwerty" requireSession="true" requireSessionWith="approve" />
<Path name="latesignin" applicationId="latesignin" authType="qwerty" requireSession="true" requireSessionWith="latesignin" />
<Path name="fblogin" applicationId="fblogin" authType="qwerty" requireSession="true" requireSessionWith="fblogin" />
</Host>
</RequestMap>
<ApplicationOverride id="retailpdf" entityID="https://login.tcxqa.hrblock.net/retailpdf/shibboleth">
<Sessions lifetime="60" timeout="20" checkAddress="false" consistentAddress="false" handlerURL="/retailpdf/Shibboleth.sso" handlerSSL="true">
<SessionInitiator type="Chaining" Location="/retailpdf" isDefault="false" id="retailpdf" forceAuthn="true" entityID="https://qaidp.hrblock.net/idp/shibboleth" authnContextClassRef="urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport">
<SessionInitiator type="SAML2" template="bindingTemplate.html" />
</SessionInitiator>
</Sessions>
您是否尝试过以下方法:
$original = Get-Content 'C:\Test\MYfile.xml'
$new = $original.replace('<Path name="retailpdf" applicationId="retailpdf" authType="qwerty" requireSession="true" requireSessionWith="retailpdf" />', '')
$new | Set-Content 'C:\Test\MYfile.xml'
如果您可以创建一个没有损坏的XML文件,那么您应该能够使用XmlDocument类中的方法删除不需要的节点。
$XMLfilepath = 'C:\Test\MYfile.xml'
$Pathxml = [xml](Get-Content $XMLfilepath)
$nodeToRemove = $Pathxml.SelectSingleNode("//Host/Path[@name = 'retailpdf']")
$nodeToRemove.ParentNode.RemoveChild($nodeToRemove)
$nodeToRemove = $Pathxml.SelectSingleNode("//ApplicationOverride[@id = 'retailpdf']")
$nodeToRemove.ParentNode.RemoveChild($nodeToRemove)
$XMLfilepathNew = 'C:\Test\Myfiledone.xml'
$Pathxml.Save($XMLfilepathNew)
请注意,您可以在XPath中使用通配符。 这将有助于更简洁地删除所有包含特定值的属性。 但是,您确实需要了解XML数据的上下文级别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.