Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Re: how to change xml values

Reply
 
Old 07-31-2008   #1 (permalink)
Kiron


 
 

Re: how to change xml values

You can use XPath notation to get the node's attribute and set its new value:

[xml]$xml = gc c:\data.xml
$xml.selectSingleNode('//add[@key="TCPport"]').value = '3000'
$xml.selectSingleNode('//add[@key="tserver"]').value = 'server9'
$xml.save('c:\data.xml') # <-- full path

--
Kiron

My System SpecsSystem Spec
Old 07-31-2008   #2 (permalink)
Kiron


 
 

Re: how to change xml values

'//add' is XPath for "select all 'add' child nodes anywhere in the xmlDocument" and '@key' means the 'key' attribute. So '//add[@key="TCPport"]' means select the 'add' node whose 'key' attribute's value is "TCPport".

If you're interested, this is a good tutorial:
http://www.w3schools.com/xpath/xpath_syntax.asp

--
Kiron
My System SpecsSystem Spec
Old 08-01-2008   #3 (permalink)
Kiron


 
 

Re: how to change xml values

In PowerShell xml nodes can be accessed through the dot operator, like object properties. Sometimes there can be more than one node with the same name, there you can use array notation.
For example, you can get all the 'add' nodes, filter the one whose 'key' attribute's value -eq 'TCPport' and set its 'value' attribute's value to '3000' this way also:

$xml.configuration.appSettings.add |
? {$_.key -eq 'TCPport'} | % {$_.value = '3000'}

# get-member can help you discover the elements
$xml | gm -t property

# the ChildNodes property lists the node's childNodes, if any
$xml.childNodes | select name
$xml.configuration.childNodes | select name

# ...etc.


I don't know if there is a .NET property/method that will display the element list, hope I'm wrong.
Something like this gets you a list of the element names:

[xml]$xml = gc c:\data.xml
$xml | fc | out-string -str | ? {$_ -match $rgx} |
% {$h = @{}; $c = 0} {$h."$($rgx.replace($_,'$1'))" = $c++}
$elements = $h.getEnumerator() | sort {[int]$_.value} |
? {$_.name -match '^\w'} | % {$_.name}
$elements

--
Kiron
My System SpecsSystem Spec
Old 08-01-2008   #4 (permalink)
Kiron


 
 

Re: how to change xml values

Forgot to include the RegEx...
[regex]$rgx = '\s+(\S+) =.+'

--
Kiron
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Lookup values PowerShell
ram wrong values Vista performance & maintenance
values VB Script
Compare values PowerShell
Getting WLX_NOTIFICATION_INFO values Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46