Welcome Forums General PowerShell Q&A Easy way to parse or convert XMLDocument datatype

Viewing 6 reply threads
  • Author
    Posts
    • #169195
      Participant
      Topics: 13
      Replies: 42
      Points: 243
      Rank: Participant

      I’m using an API that’s returning results as an XMLdocument variable. After reading online, it seemed like it would be simple to parse, but it doesn’t look like others.

      https://imgur.com/v7j2CIE

      If I expand XmlElement, I’ll see an array of more XmlElement and XmlWhitespace arrays.

      Looking under the InnerXML and OuterXML attributes, I can see the data tree but I have no clue how to parse it. I also don’t know what attributes will be returned. I found an attribute of some XmlElements named “InnerText” that seems to have some (maybe all) the information that I’m looking for, but I’d have to manually parse it myself.

      Is there a way to simply export it to a CSV or convert it to a more readable data type like an array? Trying to find my way through the array of arrays of XML objects isn’t fun.

      Not sure if this means anything to anyone, but I’m making the API call using Invoke-RestMethod

    • #169249
      Senior Moderator
      Topics: 8
      Replies: 1158
      Points: 4,045
      Helping Hand
      Rank: Community Hero

      Can you post the xml here, you can do it via gist.gihub.com. Its difficult to guess, so at least a dummy XML with same schema will also help.

    • #169276
      Participant
      Topics: 13
      Replies: 42
      Points: 243
      Rank: Participant

      gist.gihub.com

      I appreciate the reply.

      Here’s the XML with personal information removed: https://pastebin.com/g9uLmXhY

      I decided to use pastebin since it doesn’t require an account, hope that is okay.

    • #169285
      Senior Moderator
      Topics: 8
      Replies: 1158
      Points: 4,045
      Helping Hand
      Rank: Community Hero

      Fine, You can just read the content and type cast the output to XML and is the simplest method.

      [xml]$XML = Get-Content -Path  <Path to XML>
      
      #For your XML
      
      $XML.ServiceResponse.data.Asset # will give asset detail.
      
    • #169288
      Participant
      Topics: 13
      Replies: 42
      Points: 243
      Rank: Participant

      Fine, You can just read the content and type cast the output to XML and is the simplest method.

      [xml]$XML = Get-Content Path
      #For your XML
      $XML.ServiceResponse.data.Asset # will give asset detail.
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      I see, I didn’t see those as attributes of the XMLDocument object, but that worked. However, when I pipe it to a text file, I get this

      id : 123456789
      name : ComputerName
      created : 2019-05-16T21:41:12Z
      modified : 2019-08-03T14:01:01Z
      type : HOST
      tags : tags
      In other words, it’s not actually providing me the list of what’s in the “tags”. I tried this to just view the tags, but received no output.
      $XML.ServiceResponse.data.Asset.Tags.list.TagSimple
    • #169294
      Participant
      Topics: 0
      Replies: 45
      Points: 243
      Helping Hand
      Rank: Participant

      @Crusher2156

      You can combine both tag together and make it more meaningfull
      Example : 
      [xml]$bandData = Get-Content -path xml.txt
      foreach ($bands in $bandData.ServiceResponse.data.Asset) {
      write-host "======================================"
      write-host " Asset:$($bands.id) "
      write-host "======================================"
      foreach($band in $bands.tags.list.tagsimple){ 
      write-host "======================================"
      [pscustomobject]@{
      ID=$bands.id
      Name = $bands.Name
      Created = $bands.Created
      modified = $bands.modified
      Type=$bands.type
      SubID=$band.id
      SubName=$band.name
      }
      write-host "======================================"
      }
      write-host "======================================"
      }
      
      
      OutPut:
      E:\github\PowerShell\Readthexml> E:\github\PowerShell\Readthexml\readthexml.ps1
      ======================================
                  Asset:123456789        
      ======================================
      ======================================
      
      Name                           Value                                                                                                                                                   
      ----                           -----                                                                                                                                                   
      ID                             123456789                                                                                                                                               
      Name                           ComputerName                                                                                                                                            
      SubID                          12345678                                                                                                                                                
      modified                       2019-08-02T15:18:36Z                                                                                                                                    
      Type                           HOST                                                                                                                                                    
      Created                        2019-05-16T21:41:12Z                                                                                                                                    
      SubName                        NameHere                                                                                                                                                
      ======================================
      ======================================
      ID                             123456789                                                                                                                                               
      Name                           ComputerName                                                                                                                                            
      SubID                          12345678                                                                                                                                                
      modified                       2019-08-02T15:18:36Z                                                                                                                                    
      Type                           HOST                                                                                                                                                    
      Created                        2019-05-16T21:41:12Z                                                                                                                                    
      SubName                        NameHere                                                                                                                                                
      ======================================
      ======================================
      ID                             123456789                                                                                                                                               
      Name                           ComputerName                                                                                                                                            
      SubID                          12345678                                                                                                                                                
      modified                       2019-08-02T15:18:36Z                                                                                                                                    
      Type                           HOST                                                                                                                                                    
      Created                        2019-05-16T21:41:12Z                                                                                                                                    
      SubName                        NameHere                                                                                                                                                
      ======================================
      ======================================
      ID                             123456789                                                                                                                                               
      Name                           ComputerName                                                                                                                                            
      SubID                          12345678                                                                                                                                                
      modified                       2019-08-02T15:18:36Z                                                                                                                                    
      Type                           HOST                                                                                                                                                    
      Created                        2019-05-16T21:41:12Z                                                                                                                                    
      SubName                        OS - Windows 10                                                                                                                                         
      ======================================
      ======================================
      ID                             123456789                                                                                                                                               
      Name                           ComputerName                                                                                                                                            
      SubID                          12345678                                                                                                                                                
      modified                       2019-08-02T15:18:36Z                                                                                                                                    
      Type                           HOST                                                                                                                                                    
      Created                        2019-05-16T21:41:12Z                                                                                                                                    
      SubName                        NameHere                                                                                                                                                
      ======================================
      ======================================
      ID                             123456789                                                                                                                                               
      Name                           ComputerName                                                                                                                                            
      SubID                          1234567                                                                                                                                                 
      modified                       2019-08-02T15:18:36Z                                                                                                                                    
      Type                           HOST                                                                                                                                                    
      Created                        2019-05-16T21:41:12Z                                                                                                                                    
      SubName                        Cloud Agent                                                                                                                                             
      ======================================
      ======================================

       

    • #169303
      Participant
      Topics: 13
      Replies: 42
      Points: 243
      Rank: Participant

      @Crusher2156

      You can combine both tag together and make it more meaningfull
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      Example :·
      [xml]$bandData = Get-Content path xml.txt
      foreach ($bands in $bandData.ServiceResponse.data.Asset) {
      write-host “======================================”
      write-host ” Asset:$($bands.id) “
      write-host “======================================”
      foreach($band in $bands.tags.list.tagsimple){
      write-host “======================================”
      [pscustomobject]@{
      ID=$bands.id
      Name = $bands.Name
      Created = $bands.Created
      modified = $bands.modified
      Type=$bands.type
      SubID=$band.id
      SubName=$band.name
      }
      write-host “======================================”
      }
      write-host “======================================”
      }
      OutPut:
      E:\github\PowerShell\Readthexml> E:\github\PowerShell\Readthexml\readthexml.ps1
      ======================================
      Asset:123456789
      ======================================
      ======================================
      Name Value
      —- —–
      ID 123456789
      Name ComputerName
      SubID 12345678
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName NameHere
      ======================================
      ======================================
      ID 123456789
      Name ComputerName
      SubID 12345678
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName NameHere
      ======================================
      ======================================
      ID 123456789
      Name ComputerName
      SubID 12345678
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName NameHere
      ======================================
      ======================================
      ID 123456789
      Name ComputerName
      SubID 12345678
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName OS Windows 10
      ======================================
      ======================================
      ID 123456789
      Name ComputerName
      SubID 12345678
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName NameHere
      ======================================
      ======================================
      ID 123456789
      Name ComputerName
      SubID 1234567
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName Cloud Agent
      ======================================
      ======================================
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      @Crusher2156

      You can combine both tag together and make it more meaningfull
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
      Example :·
      [xml]$bandData = Get-Content path xml.txt
      foreach ($bands in $bandData.ServiceResponse.data.Asset) {
      write-host “======================================”
      write-host ” Asset:$($bands.id) “
      write-host “======================================”
      foreach($band in $bands.tags.list.tagsimple){
      write-host “======================================”
      [pscustomobject]@{
      ID=$bands.id
      Name = $bands.Name
      Created = $bands.Created
      modified = $bands.modified
      Type=$bands.type
      SubID=$band.id
      SubName=$band.name
      }
      write-host “======================================”
      }
      write-host “======================================”
      }
      OutPut:
      E:\github\PowerShell\Readthexml> E:\github\PowerShell\Readthexml\readthexml.ps1
      ======================================
      Asset:123456789
      ======================================
      ======================================
      Name Value
      —- —–
      ID 123456789
      Name ComputerName
      SubID 12345678
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName NameHere
      ======================================
      ======================================
      ID 123456789
      Name ComputerName
      SubID 12345678
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName NameHere
      ======================================
      ======================================
      ID 123456789
      Name ComputerName
      SubID 12345678
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName NameHere
      ======================================
      ======================================
      ID 123456789
      Name ComputerName
      SubID 12345678
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName OS Windows 10
      ======================================
      ======================================
      ID 123456789
      Name ComputerName
      SubID 12345678
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName NameHere
      ======================================
      ======================================
      ID 123456789
      Name ComputerName
      SubID 1234567
      modified 2019-0802T15:18:36Z
      Type HOST
      Created 2019-0516T21:41:12Z
      SubName Cloud Agent
      ======================================
      ======================================
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      That’s a lot easier than I thought. I was able to modify it to do exactly what I needed. Thank you for the help!

Viewing 6 reply threads
  • The topic ‘Easy way to parse or convert XMLDocument datatype’ is closed to new replies.