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 - XmlDocument SelectSingleNode doesn't work

Reply
 
Old 11-16-2007   #1 (permalink)
John Shwon


 
 

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 SpecsSystem Spec
Old 11-16-2007   #2 (permalink)
Shay Levi


 
 

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 SpecsSystem Spec
Old 11-16-2007   #3 (permalink)
Karl Prosser[MVP]


 
 

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 SpecsSystem Spec
Old 11-16-2007   #4 (permalink)
Shay Levi


 
 

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 SpecsSystem Spec
Reply

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


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