19

I have the following 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".

1

4 Answers 4

13

This version uses a bit more PowerShell and handles the case of mulitple items with WorkDir keys:

$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 Extensions installed you can use the Format-Xml cmdlet to format the output and save it via Out-File e.g.:

$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.

Sign up to request clarification or add additional context in comments.

Comments

11

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.

4 Comments

You can use the Select-Xml cmdlet in PowerShell 2.0 in place of the SelectSingleNode method call.
Select-Xml is available in PowerShell 1.0, however I think the above is still the easiest way to access and modify the value of an attribute.
FYI, Select-Xml is not available in PowerShell 1.0. It does come with the PowerShell Community Extensions which is probably why you are seeing it in 1.0.
Yeah. We had the problem of writing deploy scripts - I take it Community Extensions would need to be installed on the deploy box before we could run the deploy if we wanted Format-Xml. With Save though, using $pwd is super useful for getting behavior that mirrors Get-Content.
6

You can load your XML into a LINQ XDocument class from PowerShell like this:

[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 XmlDocument class in a similar way.

1 Comment

Keep in mind that PowerShell doesn't support the C# 3.0 static extension method syntax where you can invoke the ext method on an instance. And IIRC, PowerShell doesn't support invoking generic methods either. So, as long as you're just creating XLINQ objects you're OK but if you attempt to use the LINQ query capability (System.Linq.Enumerable), it is going to be an exercise in futility.
0
$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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.