Windows Vista Forums

Listing of all elements in XML
  1. #1


    Marco Shaw Guest

    Listing of all elements in XML

    39# gc xml.test
    <data>
    <data1>
    testing
    </data1>
    </data>
    40# [xml]$test=gc xml.test

    Is there an easy way that I can get something outputted contained the
    contents of each element?

    Like:
    element text
    ------- ----
    data <data1>testing</data1>
    data1 testing

    I know I can do it by hand, but I'm looking for something to iterate through
    all the elements.

    Marco





      My System SpecsSystem Spec

  2. #2


    Keith Hill [MVP] Guest

    Re: Listing of all elements in XML

    "Marco Shaw" <marcoDOTshaw_@_gmailDOTcom> wrote in message
    news:e2ksiUaYHHA.4264@TK2MSFTNGP05.phx.gbl...
    > 39# gc xml.test
    > <data>
    > <data1>
    > testing
    > </data1>
    > </data>
    > 40# [xml]$test=gc xml.test
    >
    > Is there an easy way that I can get something outputted contained the
    > contents of each element?
    >
    > Like:
    > element text
    > ------- ----
    > data <data1>testing</data1>
    > data1 testing
    >
    > I know I can do it by hand, but I'm looking for something to iterate
    > through all the elements.
    >


    I think this will work for ya:

    $test.SelectNodes('//*') | select @{n='element';e={$_}},
    @{n='text';e={$_.get
    _InnerXml()}} | fl

    --
    Keith



      My System SpecsSystem Spec

  3. #3


    RichS Guest

    RE: Listing of all elements in XML

    Have you looked at format-xml in the powershell community extensions
    --
    Richard Siddaway
    Please note that all scripts are supplied "as is" and with no warranty
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "Marco Shaw" wrote:

    > 39# gc xml.test
    > <data>
    > <data1>
    > testing
    > </data1>
    > </data>
    > 40# [xml]$test=gc xml.test
    >
    > Is there an easy way that I can get something outputted contained the
    > contents of each element?
    >
    > Like:
    > element text
    > ------- ----
    > data <data1>testing</data1>
    > data1 testing
    >
    > I know I can do it by hand, but I'm looking for something to iterate through
    > all the elements.
    >
    > Marco
    >
    >
    >


      My System SpecsSystem Spec

  4. #4


    Brandon Shell Guest

    Re: Listing of all elements in XML

    You have that in english

    "Keith Hill [MVP]" <r_keith_hill@no.spam.thank.u.hotmail.com> wrote in
    message news:uDhAI5aYHHA.4940@TK2MSFTNGP05.phx.gbl...
    > "Marco Shaw" <marcoDOTshaw_@_gmailDOTcom> wrote in message
    > news:e2ksiUaYHHA.4264@TK2MSFTNGP05.phx.gbl...
    >> 39# gc xml.test
    >> <data>
    >> <data1>
    >> testing
    >> </data1>
    >> </data>
    >> 40# [xml]$test=gc xml.test
    >>
    >> Is there an easy way that I can get something outputted contained the
    >> contents of each element?
    >>
    >> Like:
    >> element text
    >> ------- ----
    >> data <data1>testing</data1>
    >> data1 testing
    >>
    >> I know I can do it by hand, but I'm looking for something to iterate
    >> through all the elements.
    >>

    >
    > I think this will work for ya:
    >
    > $test.SelectNodes('//*') | select @{n='element';e={$_}},
    > @{n='text';e={$_.get
    > _InnerXml()}} | fl
    >
    > --
    > Keith
    >



      My System SpecsSystem Spec

  5. #5


    Keith Hill [MVP] Guest

    Re: Listing of all elements in XML

    "Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message news:OaBgXfdYHHA.3996@TK2MSFTNGP02.phx.gbl...
    > You have that in english


    :-)

    $test.SelectNodes('//*') |
    select @{n='element';e={$_}},
    @{n='text'; e={$_.get_InnerXml()}} |
    fl

    First we use an XPath expression '//*' that will select every node. Pass that to the SelectNodes() method on the XmlDocument object. Now those nodes are passed down the pipeline. We want to pick off the node's element/tag name represented by $_ as well as the XML contained within that element. We get that info with $_.get_InnerXml(). The rest is just hashtable goo to get Select-Object to display these expressions with the column titles that I want.

    --
    Keith

      My System SpecsSystem Spec

  6. #6


    Brandon Shell Guest

    Re: Listing of all elements in XML

    Really.. I was joking, but the explanation was very nice... Thanks.
    "Keith Hill [MVP]" <r_keith_hill@no.spam.thank.u.hotmail.com> wrote in message news:utbmHTeYHHA.4008@TK2MSFTNGP05.phx.gbl...
    "Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message news:OaBgXfdYHHA.3996@TK2MSFTNGP02.phx.gbl...
    > You have that in english


    :-)

    $test.SelectNodes('//*') |
    select @{n='element';e={$_}},
    @{n='text'; e={$_.get_InnerXml()}} |
    fl

    First we use an XPath expression '//*' that will select every node. Pass that to the SelectNodes() method on the XmlDocument object. Now those nodes are passed down the pipeline. We want to pick off the node's element/tag name represented by $_ as well as the XML contained within that element. We get that info with $_.get_InnerXml(). The rest is just hashtable goo to get Select-Object to display these expressions with the column titles that I want.

    --
    Keith

      My System SpecsSystem Spec

  7. #7


    Doug Guest

    Re: Listing of all elements in XML

    A very instructive example. Thanks
    "Keith Hill [MVP]" <r_keith_hill@no.spam.thank.u.hotmail.com> wrote in message news:utbmHTeYHHA.4008@TK2MSFTNGP05.phx.gbl...
    "Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message news:OaBgXfdYHHA.3996@TK2MSFTNGP02.phx.gbl...
    > You have that in english


    :-)

    $test.SelectNodes('//*') |
    select @{n='element';e={$_}},
    @{n='text'; e={$_.get_InnerXml()}} |
    fl

    First we use an XPath expression '//*' that will select every node. Pass that to the SelectNodes() method on the XmlDocument object. Now those nodes are passed down the pipeline. We want to pick off the node's element/tag name represented by $_ as well as the XML contained within that element. We get that info with $_.get_InnerXml(). The rest is just hashtable goo to get Select-Object to display these expressions with the column titles that I want.

    --
    Keith

      My System SpecsSystem Spec

Listing of all elements in XML problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Not listing sent mail Ken H Vista mail 1 19 Mar 2010
Adobe Photoshop Elements 6 & Adobe Premiere Elements 4 Jorge131 Vista music pictures video 17 30 Jan 2010
Directory Listing Ken VB Script 3 10 Jul 2008
Can't delete elements in folder Recent elements Gudmund Liebach Nielsen Vista General 1 05 May 2008
Support for Adobe photoshop elements 5 or premier elements 3 on Vista 64 bits Sébastien DELAYRE Vista General 5 26 Mar 2007