有人可以幫助我更有效地執行以下操作嗎?我運行此代碼 4 次以更新特定的以下協議/檔案型別。我想以回圈或其他方式來提高效率!
$XMLPATH = 'C:\DefaultAssociation.xml'
$xml = [xml](Get-Content $XMLPATH -raw)
#update .htm
$htm = ($xml.DefaultAssociations.Association | where-Object Identifier -eq '.htm')
$htm.SetAttribute('ProgId', "ChromeHTML")
$htm.SetAttribute('ApplicationName', "Google Chrome")
#update .html
$html = ($xml.DefaultAssociations.Association | where-Object Identifier -eq '.html')
$html.SetAttribute('ProgId', "ChromeHTML")
$html.SetAttribute('ApplicationName', "Google Chrome")
#update .https
$https = ($xml.DefaultAssociations.Association | Where-Object Identifier -eq 'https')
$https.SetAttribute('ProgId', "ChromeHTML")
$https.SetAttribute('ApplicationName', "Google Chrome")
#update http
$http = ($xml.DefaultAssociations.Association | Where-Object Identifier -eq 'http')
$http.SetAttribute('ProgId', "ChromeHTML")
$http.SetAttribute('ApplicationName', "Google Chrome")
$XML.SAVE($XMLPATH)
而不是多次運行它,我需要將其設為陣列或某種回圈,因此它不會多次讀取 XML 檔案。如何更有效地做到這一點?
uj5u.com熱心網友回復:
使用 PowerShell 的-in運算子在單個操作中對多個值進行相等測驗,任何成功的測驗都會使進一步的測驗短路并回傳$true整體:
$htm = $xml.DefaultAssociations.Association |
Where-Object Identifier -in '.htm', '.html', 'https', 'http'
$htm.SetAttribute('ProgId', "ChromeHTML")
$htm.SetAttribute('ApplicationName', "Google Chrome")
一個簡單的例子來演示如何-in作業:
'Foo' -in 'bar', 'foo', 'baz'產生$true,并且是簡明的等價于:
'Foo' -eq 'bar' -or 'Foo' -eq 'foo' -or 'Foo' -eq 'baz'
與 PowerShell 中的所有運算子一樣-in,默認情況下不-eq區分大小寫;為了區分大小寫,請使用它們的c- 前綴變體,即-cin和-ceq.
順便說一句$xml = [xml](Get-Content $XMLPATH -raw):
以這種方式加載 XML 檔案并不完全可靠;請改用以下成語:
($xml = [xml]::new()).Load((Convert-Path -LiteralPath $XMLPATH))有關背景資訊,請參閱此答案。
uj5u.com熱心網友回復:
要在回圈中執行陳述句,您可以執行以下操作:盡管如我的評論中所述,它不會給您帶來性能優勢。
$XMLPATH = 'C:\DefaultAssociation.xml'
$xml = [xml](Get-Content $XMLPATH -raw)
$extensions = @(".htm",".html",".http",".https")
foreach($extension in $extensions){
$val = ($xml.DefaultAssociations.Association | where-Object Identifier -eq $extension)
$val.SetAttribute('ProgId', "ChromeHTML")
$val.SetAttribute('ApplicationName', "Google Chrome")
}
$XML.SAVE($XMLPATH)
uj5u.com熱心網友回復:
考慮XSLT,一種專門用于轉換或設定 XML 檔案樣式的語言。使用這種方法,您只需讀取一次 XML 并處理所有更改。另外,您可以添加其他需要的更改。PowerShell 可以連接到System.Xml.Xsl.XslCompiledTransform
XSLT (另存為 .xsl,一個特殊的 .xml 檔案)
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<!-- Identity Transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Adjust Identifier Attributes -->
<xsl:template match="Identifier[text()='htm' or text()='html' or text()='https' or text()='http']">
<xsl:copy>
<xsl:attribute name="ProgId">ChromeHTML</xsl:attribute>
<xsl:attribute name="ApplicationName">Google Chrome</xsl:attribute>
<xsl:apply-templates select="@*[name()!='ProgId' and name()!='ApplicationName']"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:transform>
電源外殼
$xml = "C:\DefaultAssociation.xml";
$xsl = "C:\Path\To\Script.xsl";
$output = "C:\Path\To\Output.xml";
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform;
$settings = New-Object System.Xml.Xsl.XsltSettings($true, $true);
$resolver = New-Object System.Xml.XmlUrlResolver;
$xslt.Load($xsl, $settings, $resolver);
$xslt.Transform($xml, $output);
Online Demo (使用虛擬 XML 和相同的 MS XslCompiledTransform 類)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471880.html
上一篇:無法對沒有年份的員工串列進行分組
