![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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? |
My System Specs![]() |
| | #2 (permalink) |
| | 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 Quote: > 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? > |
My System Specs![]() |
| | #3 (permalink) |
| | 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: Quote: > 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? |
My System Specs![]() |
| | #4 (permalink) |
| | 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 Quote: > 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: > Quote: >> 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? >> |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Generally accepted file size threshold of XmlDocument? | .NET General | |||
| XmlDocument.Save() with null XmlResolver modifies DOCTYPE tag | .NET General | |||
| Xmldocument encoding question. | .NET General | |||
| Does the XMLDocument.Load(System.IO.Stream) method load the whole | .NET General | |||
| ADO and XmlDocument cmdlets? | PowerShell | |||