Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 790 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selecting attributes in xml using xpath in powershell

#1
I am trying to use powershell and XPath to select the name attribute shown in the below xml example.

$xml_peoples= $file.SelectNodes("//people")
foreach ($person in $xml_peoples){
echo $person.attributes
#echo $person.attributes.name
}

Above is the code im running to try and get the name, but it doesn't seem to work. Any suggestions?

<peoples>
<person name='James'>
<device>
<id>james1</id>
<ip>192.192.192.192</ip>
</device>
</person>
</peoples>

Thanks in advance!
Reply

#2
I'm not sure what **$hub** is, and you started your code from the middle so it's not clear if you properly set **$file** to an XmlDocument object, but I think this is what you want:

[System.Xml.XmlDocument]$file = new-object System.Xml.XmlDocument
$file.load(<path to XML file>)
$xml_peoples= $file.SelectNodes("/peoples/person")
foreach ($person in $xml_peoples) {
echo $person.name
}
Reply

#3
These two lines should suffice:

[xml]$xml = Get-Content 'C:\path\to\your.xml'
$xml.selectNodes('//person') | select Name
Reply

#4
How about one line?

<code>Select-XML -path "pathtoxml" -xpath "//person/@name"</code>
Reply

#5
For anyone that has to work around Select-Xml's garbage namespace handling, here's a one-liner that doesn't care, as long as you know the direct path:

([xml](Get-Content -Path "path\to.xml")).Peoples.Person.Name

The above will return all matching nodes as well. It's not as powerful, but it's clean when you know the schema and want one thing out of it quickly.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through