Powershell XML: Attributes present, or not, changes how to get the Value -
I am using PowerShell 3.0, on Windows 8 PC. I have a simple XML file with such content (without the fobar part, which comes later):
Now, I am removing all the versions in one array:
[xml] $ FileData = Get-content \ Thefile.xml [array] $ theVersions = $ FileData.MyData.Version | | Everyone is working fine, by then I have added foobar = "1" to the XML file - I have dug a bit of everything that seems to break, and # Tried things related to text and intertext: [array] $ theVersions = $ FileData.MyData.version | % {$ _. '# Text'} But it only works with one piece with the phone; The second two are invisible Naturally, I can add some (any) attribute to all the data in the file, but this is not a practical solution. My question: is a feature that has or not a value (or #text, or intertext, whatever you call it from those XML nodes To read) in a consistent way is current? Actually, the presence or absence of any attribute should be considered unknown on my script. Follow-up: Thanks for Keith's suggestion, and tweaking a little bit, I was able to choose to over-control my XML-using trouble. As I need that feature when presently (I had left that statement out in the beginning because I did not think that it matters), I have formatted something like this: [xml] $ FileData = Go-content \ Thefile.xml [array] $ theVersions = (Selection-XML $ FileData- XPath '//MyData/Version').Node | % {If $$ _ Foobar -ne $ null} {"$ ($ _. '# Text') and forber $ ($ _ $ foobar)"} and {$ _. '# Text'}} $ theVersions and the resulting output:
1.2a 3.4b and Foobar 1 5.6c Thanks, everybody! Hope this question and solution helps someone else!
If you want to use -xml , it is that you How to do this: $ xml = [xml] @ '& lt; MyData> & Lt; Version & gt; 1.2A & lt; / Edition & gt; & Lt; Version foobar = "1" & gt; 3.4b & lt; / Edition & gt; & Lt; Version & gt; 5.6c & lt; / Edition & gt; & Lt; / MyData & gt; '@ (Selection-Xml $ xml -XPath' //Version/text()').Node.Value Any element containing this name will be version , If you want to limit it to only under MyData , replace XPath with / MyData / version / text () . It assumes that MyData is the root / doctor element.
Comments
Post a Comment