![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | XmlDocument SelectSingleNode doesn't work I'm trying to parse the XML snippet below using PowerShell. <Envelope> <Body>hello world</Body> </Envelope> The C# code below works perfectly and prints "Hello World" as expected. XmlDocument doc = new XmlDocument(); doc.Load(@"C:\test.xml"); string str = doc.SelectSingleNode("//Body").InnerText; Console.WriteLine(str); Trying the following in PowerShell doesn't seem to work: $doc = new-object "System.Xml.XmlDocument" $doc.Load("c:\test.xml") $doc.SelectSingleNode("//Body").InnerText; Where am I going wrong? |
| | #2 (permalink) | ||||||||||||
| Guest | Re: XmlDocument SelectSingleNode doesn't work You can get the node text by: PS > $doc = new-object "System.Xml.XmlDocument" PS > $doc.Load("c:\test.xml") PS > $doc.Envelope.Body.InnerText; hello world If you have multiple body elements: PS > $xml=[xml]"<Envelope><Body>hello world</Body><Body>hello world1</Body></Envelope>" PS > $xml.Envelope.Body hello world hello world1 PS > $xml.Envelope.Body.Count 2 PS > $xml.Envelope.Body[0] hello world PS > $xml.Envelope.Body[1] hello world1 As for the SelectSingleNode member (altough it is visible to $xml), not sure, I think you need to use XPath ([System.XML.XPath.XPathDocument]) HTH ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com
| ||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | Re: XmlDocument SelectSingleNode doesn't work Xmldocument is a type that Powershell adapts. so it can do some special things on it.. for my example i'll use your same document but i won't load it from file i will create it with a typecast but it should work the same $doc = [xml]'<Envelope><Body>hello world</Body></Envelope>' if you do $doc.gettype()you'll notice its still a system.xml.Xmldocument however some of the properties of that class are hidden from powershell are some more added. Notably if you do $doc | get-member you'll see an Envelope. Powershell actually adapts the contents of hte xml document as properties so you can simply do.. $doc.envelope or even $doc.envelope.body which will return the XmlNodes XmlElements etc. however what if you happened to have an xml node called innertext, you'd have a conflict, thus powershell hides a bunch of the build in properties if you run $doc.SelectSingleNode("//Body") you'll notice you actually get results in your console. its just you can't access the innertext.. well with adapted types in powershell you can always get the underlying dotnet objects properties and methods with ..psbase $doc.SelectSingleNode("//Body").psbase.innertext will do the job but also you could do $doc.SelectSingleNode("//Body").get_Innertext() or a property powershell adds #text $doc.SelectSingleNode("//Body")."#text" Hope this helps. John Shwon wrote:
| ||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||
| Guest | Re: XmlDocument SelectSingleNode doesn't work Karl, Thanks for the psbase reminder :-) PowerShell v2 includes Updated Type Adapters. This removes the need to use PSBASE property to access base members. PSABSE members will be directly accessible on the object istself without the need to query the raw object. ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com
| ||||||||||||||||||||||||
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Still searching for way to get Vista search to work: Why doesn't FilterFilesWithUnknownExtensions registry key work in Vista? | Celegans | Vista General | 11 | 08-31-2007 09:04 AM |
| all cd's don't work, but dvd's still work | Phil | Vista hardware & devices | 2 | 05-18-2007 09:35 AM |
| ADO and XmlDocument cmdlets? | Kevin Burton | PowerShell | 5 | 11-21-2006 01:56 PM |
| HP LaserJet 1010 don't work in Vista since Beta2 - Advanced 1384 Printing Support drivers for XP don't work | Alexey | Vista print fax & scan | 2 | 10-18-2006 07:05 PM |
| Will all the programs that work on xp work on vista? | exciter | Vista General | 14 | 10-06-2006 11:40 PM |