Comments are closed for this post.
Posted by
Phil Fearon
@
27 Jan 2008
10:05 AM
XPath. A great way for extracting xml config data stored as xml for a powershell script. A couple of points -
A single-line alternative to:
$xml = New-Object "System.Xml.XmlDocument"
$xml.load("c:\myfile.xml")
would be:
$xml = [xml] (get-content "c:\myfile.xml")
Also, one thing that threw me at first was that the returned XmlNode(s) is 'wrapped' by PowerShell. So, to access basic properties of XmlNode, I had to use the 'PSBase' member as in:
$value = $node.psbase.innertext
Posted by
Josh
@
02 Dec 2008
7:35 AM
Cheers Phil,
Nice improvement using get-content. The alternative to using psbase is to use the getter method directly, e.g.:
$node.get_InnerText()
Josh
Posted by
Jeff
@
26 Jan 2009
5:37 PM
No Joke Phil, I just found out about the PSBase concept from reading this article and I was getting quite frustrated trying to use .Value and .InnerText and such until I just couldn't take it any more and stated googling.
Posted by
RobLundkvist
@
31 Mar 2009
3:35 AM
I ran into the psbase issue as well, thanks :)
Posted by
Dima
@
31 Aug 2010
5:27 PM
Just wish to give couple more examples.
read file another format;
[xml] $foo = gc c:\temp\blah.xml
Setting values using xpath wilds:
$xpath = "//*[@name='nameOfNode']/value"
$foo.SelectSingleNode($xpath).set_InnerText("BLAH")
much more readable the hash aproache:
[xml] $foo = "<?xml version='1.0'?><authorization><allow roles='myDomain\Domain Users' /><deny users='*' /></authorization>"
$foo.authorization.allow.roles = "new\Domain Users"
keep one thing in mind, new value will have to be a string, it does not do well with anything else... so convert to string first
Finally, biggest pain is the Namespaces, you can spend hours researching it.
In Hibernate i see:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
you have to create a new namespace object:
$ns = New-Object Xml.XmlNamespaceManager $foo.NameTable
$ns.AddNamespace( "nh", "urn:nhibernate-configuration-2.2" )
$xpath = "//nh:property[@name='connection.connection_string']"
$node = $xml.SelectSingleNode($xpath, $ns)
good luck with the last one
Posted by
Hartmut
@
17 Aug 2011
9:45 AM
This threat was very helpful for me. But still I'm in trouble with the namespace stuff.
$ns = New-Object Xml.XmlNamespaceManager $foo.NameTable
For what stands the variable $foo.NameTable?
Thanks
Hartmut