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 - How do I read a XML file into a hash table?

Reply
 
Old 10-14-2006   #1 (permalink)
wangxiaohu


 
 

How do I read a XML file into a hash table?

Examples needed.

Thanks!

My System SpecsSystem Spec
Old 10-14-2006   #2 (permalink)
dreeschkind


 
 

RE: How do I read a XML file into a hash table?

Just a quick hack:

PS>
"<hash><entry><key>foo</key><value>1</value></entry><entry><key>bar</key><value>2</value></entry></hash>" | out-file hashXML.txt
PS> $hashXML = [xml] (get-content hashXML.txt)
PS> $hashXML.hash.entry | % { $hash = @{} } { $hash += @{$_.key = $_.value} }
PS> $hash

Name Value
---- -----
bar 2
foo 1

--
greetings
dreeschkind

"wangxiaohu" wrote:

> Examples needed.
>
> Thanks!

My System SpecsSystem Spec
Old 10-17-2006   #3 (permalink)
Keith Hill [MVP]


 
 

Re: How do I read a XML file into a hash table?

"wangxiaohu" <wangxiaohu@discussions.microsoft.com> wrote in message
newsAC2BF59-B9C8-404E-AAEC-04FA61428085@microsoft.com...
> Examples needed.


Unless every element name is unique i.e. maxOccurs is never more than 1, I
don't see how this would be helpful? Hashtables perform fast lookups based
off of a unique key, if the key isn't unique then a hashtable is a good fit.
Did you know that if you just convert a string to [xml] you get a full
System.Xml.XmlDocument object? For example:

PS> $xml =
[xml]"<doc><element1>Foo</element1><element1>Bar</element1></doc>"
PS> $xml.doc

element1
--------
{Foo, Bar}

PS> $xml.doc.element1[0]
Foo

PS>$xml.GetElementsByTagName("element1")

#text
-----
Foo
Bar

Or you could use XPath expressions:

PS>$xml.SelectNodes("//element1")

#text
-----
Foo
Bar

--
Keith


My System SpecsSystem Spec
Old 10-19-2006   #4 (permalink)
Keith Hill [MVP]


 
 

Re: How do I read a XML file into a hash table?

"Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospam> wrote in message
news:%235xu8ia8GHA.2120@TK2MSFTNGP03.phx.gbl...
> "wangxiaohu" <wangxiaohu@discussions.microsoft.com> wrote in message
> newsAC2BF59-B9C8-404E-AAEC-04FA61428085@microsoft.com...
>> Examples needed.

>
> Unless every element name is unique i.e. maxOccurs is never more than 1, I
> don't see how this would be helpful? Hashtables perform fast lookups
> based off of a unique key, if the key isn't unique then a hashtable is a
> good fit.


Doh! That should be "if the key isn't unique then a hashtable is *not* a
good fit".

--
Keith


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Sorting a Hash Table before outputting it to html PowerShell
How to create a hash table from an array PowerShell
Adding data to a hash table PowerShell
Passing hash table by reference PowerShell
Variable as hash table issue 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