Question:
I have a XML document:
1 2 3 4 5 6 |
and a powershell script accessing data from that document. I need to iterate by the children of Root and print the element names of its children. Example:
1 2 3 4 5 6 7 8 9 |
$xml = [xml](gc MyXmlFile.xml); $xml.Root.Name # prints "Root" $xml.Root.ChildNodes | foreach { $_.Name } # prints 1 2 3 because Item(A|B|C) have an attribute named "Name" # I need to print ItemA ItemB ItemC |
Update: As MrKWatkins correctly pointed out below in this case I could use the LocalName property instead. However this will not work if I’m using namespaces of if I also have a LocalName attribute in my XML. I would like to know if exists a solution for this problem that always works no matter the XML file.
Answer:
You can do something like this:
1 2 |
$xml.Root | gm -MemberType property | select Name |