xml help

This topic contains 4 replies, has 3 voices, and was last updated by

 
Senior Moderator
1 week, 6 days ago.

  • Author
    Posts
  • #171001

    Participant
    Topics: 24
    Replies: 50
    Points: 84
    Rank: Member

    in this xml, what are FTP, SSHFTP, and Share considered/called, and how would I extract them with powershell? in other words, if I wanted to populate an array with type, Id, Name, where "type" is ftp, sshftp, or share, how would I get the type with powershell?

    I've imported it with get-content as [xml] and I can get to $xml.settings.hosts.

    from there, if I do $xml.settings.hosts.ftp, I see a chart (looks like an array, but get-member tells me it's an xmlelement) with all the ftp IDs and Names, but I don't actually see the label "ftp" anywhere. and the parentnode is "hosts," not "ftp."

  • #171031

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,438
    Helping Hand
    Rank: Community Hero

    FTP here is a not a type, its an XML element even if its a type your purpose. You can use get_name() method which will give the element name.

  • #171034

    Participant
    Topics: 0
    Replies: 44
    Points: 235
    Helping Hand
    Rank: Participant

    convert your xml to PScutomerobject then you will get more flexibility to use it
    Example: (Easy way as I don't know how you going to use this xml file ) as below example below 3 PowerShell objects created
    $PSObjectftp,$PSObjectSSHFTP,$PSObjectShare

    ===========================================================================================

    $Share=@()
    $SSHFTP=@()
    $FTP=@()
    
    [xml]$xml=gc -path "d:\abc.xml"
    $ftp+=$xml.GetElementsByTagName('FTP')
    $SSHFTP+=$xml.GetElementsByTagName('SSHFTP')
    $Share+=$xml.GetElementsByTagName('Share')
    
    $PSObjectftp = New-Object PSObject
    foreach ($Property in $ftp) {
    $PSObjectftp | Add-Member NoteProperty $Property.id $Property.name
    }
    
    $PSObjectSSHFTP = New-Object PSObject
    foreach ($Property in $SSHFTP) {
    $PSObjectSSHFTP| Add-Member NoteProperty $Property.id $Property.name
    }
    
    $PSObjectShare = New-Object PSObject
    foreach ($Property in $Share) {
    $PSObjectShare | Add-Member NoteProperty $Property.id $Property.name
    }
  • #175042

    Participant
    Topics: 24
    Replies: 50
    Points: 84
    Rank: Member

    get_name() got the job done. I didn't see that in the list of methods when I did get-member. i'm sure there's a good reason for that. I see these.

    GetAttribute
    GetAttributeNode
    GetElementsByTagName
    GetEnumerator
    GetHashCode
    GetNamespaceOfPrefix
    GetPrefixOfNamespace
    GetType

     

  • #175051

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,438
    Helping Hand
    Rank: Community Hero

    do Get-Member with -Force

You must be logged in to reply to this topic.