Windows Vista Forums

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

  1. #1


    wangxiaohu Guest

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

    Examples needed.

    Thanks!

      My System SpecsSystem Spec

  2. #2


    dreeschkind Guest

    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

  3. #3


    Keith Hill [MVP] Guest

    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

  4. #4


    Keith Hill [MVP] Guest

    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

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

Similar Threads
Thread Forum
Re: Hash Table not being returned properly
I haven't tried your code... By messed up, do you mean where it shows "FALSE"? Marco
PowerShell
Hash Table not returning properly in Function
Hi, I have an issue where I am trying to return the contents of a hash table from a function into my script. I am using two functions: The main...
PowerShell
Sorting a Hash Table before outputting it to html
I have a script that reads a file that contains user log in and log out data. I have a PS script that reads this on the fly for the current...
PowerShell
How to create a hash table from an array
I'm looking for the simplest way to turn an array of custom objects into a hashtable with some particular property of the custom object being the...
PowerShell
Adding data to a hash table
I have imported a csv using import-csv $clcode = import-csv $csvlocation | select clcode $clcode results are a list of 6 digit numbers, for the...
PowerShell
Passing hash table by reference
Hi, Could someone show me the correct way to pass a hash table by reference to a function in powershell so i can access the data populated in...
PowerShell
Variable as hash table issue
I've got the "space invaders" PS script on the net as an example, and I saw we can declare variable as has table to group similar variable. ...
PowerShell