xml 在powershell中解析xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1720673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse xml in powershell
提问by Trondh
I have the following xml:
我有以下xml:
<?xml version="1.0" encoding="UTF-8"?>
<sections>
<section name="Options">
<item key="HLVersionControlWebServiceURL" value="http://www.personec.no/webservices/HLVersionControl/HLVersionControl.asmx" />
<item key="AltinnWebServiceURL" value="https://www.altinn.no/webservices/DataExchange.asmx" />
<item key="WorkDir" value="F:\Altinn\Work\" />
<item key="CatalogDir" value="F:\Altinn\Work\" />
</section>
<section name="Users">
<item key="1" value="Admin" name="Administrator" fNr="" password="" entsystype="1" entsysid="180967" entsyspassword="" lastLogin="20091111161516" allowra0500="1" allowrf1037="1" allowra01821="1" allowra01822="0" allowrf1015="1" altinnuserpassword="/qwHHYwYinE=" />
</section>
<section name="SchemaTypes">
<item key="RF1037" displayname="Terminoppgave" inputdir="F:\Altinn\Work\" validationschema=".\melding-669-8570.xsd" isSubForm="0" isSignable="0" />
<item key="RA0500" displayname="SSB L?nnsstatistikk" inputdir="C:\Program Files (x86)\Personec\Altinn Monitor\Work\" validationschema=".\melding-868-7612.xsd" isSubForm="0" isSignable="0" />
<item key="RA01821" displayname="SSB Frav?rsstatistikk bedrift" inputdir="C:\Program Files (x86)\Personec\Altinn Monitor\Work\" validationschema=".\melding-862-6190.xsd" isSubForm="0" isSignable="0" />
<item key="RF1015" displayname="?rsoppgave m/ LTO" inputdir="C:\Program Files (x86)\Personec\Altinn Monitor\Work\" validationschema=".\melding-210-7928.xsd" orid="210" orversion="7928" isSubForm="0" isSignable="1" />
<item key="RF1015U" displayname="" inputdir="" validationschema=".\melding-1083-7930.xsd" orid="1083" orversion="7930" isSubForm="1" isSignable="1" />
</section>
</sections>
And I need to alter the item key WorkDir in Powershell. When using "regular" xml-read I get to the top sections (options, users, and so on) but not the "item key" nodes within each. How can I edit the value for WorkDir in powershell? (I realize I could just use a dirty string replace but I'd rather do it "properly".
我需要在 Powershell 中更改项目键 WorkDir。使用“常规” xml-read 时,我会到达顶部部分(选项、用户等),但不会到达每个部分中的“项目键”节点。如何在 powershell 中编辑 WorkDir 的值?(我意识到我可以只使用脏字符串替换,但我宁愿“正确地”执行此操作。
采纳答案by Keith Hill
This version uses a bit more PowerShell and handles the case of mulitple items with WorkDir keys:
此版本使用更多的 PowerShell 并处理具有 WorkDir 键的多个项目的情况:
$xml = [xml](Get-Content foo.xml)
$xpath = "/sections/section/item[@key='WorkDir']"
Microsoft.PowerShell.Utility\Select-Xml $xml -XPath $xpath |
Foreach {$_.Node.SetAttribute('value', $pwd)}
$xml.Save("$pwd\bar.xml")
Note, if you have the PowerShell Community Extensionsinstalled you can use the Format-Xml cmdlet to format the output and save it via Out-File e.g.:
请注意,如果您安装了PowerShell 社区扩展,则可以使用 Format-Xml cmdlet 来格式化输出并通过 Out-File 保存它,例如:
$xml | Format-Xml -AttributesOnNewLine | Out-File bar.xml -enc utf8
OTOH $xml.Save() is easier except that you must remember that it probably doesn't have the correct current dir if you were to specify just the filename. That's why I used "$pwd\bar.xml" in the first example. This is not an issue with PowerShell cmdlets like Out-File.
OTOH $xml.Save() 更简单,但您必须记住,如果您只指定文件名,它可能没有正确的当前目录。这就是我在第一个示例中使用“$pwd\bar.xml”的原因。对于像 Out-File 这样的 PowerShell cmdlet,这不是问题。
回答by Keith Hill
You could try this
你可以试试这个
$xmlFile = "d:\sample.xml"
[xml]$doc = Get-Content $xmlFile
$node = $doc.SelectSingleNode("/sections/section/item[@key='WorkDir']")
$node.Value = "New-Value"
$doc.Save($xmlFile)
You'll still be using some .Net classes and an XPath query to select the node.
您仍将使用一些 .Net 类和 XPath 查询来选择节点。
回答by GraemeF
You can load your XML into a LINQ XDocumentclass from PowerShell like this:
您可以XDocument像这样从 PowerShell将 XML 加载到 LINQ类中:
[Reflection.Assembly]::LoadWithpartialName("System.Xml.Linq") | Out-Null
$xDoc = [System.Xml.Linq.XDocument]::Parse($myXmlString)
From there you can use the usual LINQ to XML methods to replace the attribute as in this example. If you prefer you could use the older XmlDocumentclass in a similar way.
从那里您可以使用通常的 LINQ to XML 方法来替换本示例中的属性。如果您愿意,可以XmlDocument以类似的方式使用旧类。
回答by Anuj Agnihotry
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
[xml]$userfile = Get-Content $directorypath\MainSetting.xml
$Value = $userfile.GetElementsByTagName("node")
Foreach ($ValueName in $Value)
{
$FinalValue=$ValueName.InnerXML
}

