locked
Remove element from XML RRS feed

  • Question

  • I have an XML file with this format 

    <?xml version="1.0" encoding="utf-8"?>
    <Locations>
      <Location>
        <Env>DEV</Env>
        <Name>Dev Test 1</Name>
        <From>C:\temp\Test\*.*</From>
        <To>C:\temp\test\DEV</To>
      </Location>
      <Location>
        <Env>DEV</Env>
        <Name>Dev Test 2</Name>
        <From>C:\temp\Test\*.xml</From>
        <To>C:\temp\test\DEV</To>
      </Location>
    </Locations>

    I am trying to remove everything between <Location> and <Location> where the Node value of the Element is <Name>Dev Test 1<Name>.  I can remove the <Name> Element from each Node with this Code:

    $Input = "C:\Temp\test\locations.XML"
    $Output = "C:\Temp\test\newlocations.xml"
    $xml=[xml](Get-Content $Input)
    $n=$xml.SelectSingleNode('//Name')
    $n.ParentNode.RemoveChild($n)
    $xml.Save($Output)

    But need to be able to iterate through the file and only remove that Node that contains <Name>Dev Test 1<Name> and end up with a xml like:

    <?xml version="1.0" encoding="utf-8"?>
    <Locations>
         <Env>DEV</Env>
        <Name>Dev Test 2</Name>
        <From>C:\temp\Test\*.xml</From>
        <To>C:\temp\test\DEV</To>
      </Location>
    </Locations>

    Thursday, July 16, 2020 4:52 AM

Answers

All replies

  • Just use XPath to get teh node by its name and then reference the ParentNode and use the Remove method to remove it.

    The XML posted as your result is impossible.  You cannot have XML with that structure.

    Here is a link that will help you understand how to work with XML:

    https://www.w3schools.com/xml/default.asp


    \_(ツ)_/

    Thursday, July 16, 2020 4:57 AM
  • $n = $xml.SelectSingleNode('//Name[text()="Dev Test 1"]')
    
    The link will show you how to use XPath queries.

    \_(ツ)_/

    • Marked as answer by Capn Steve Tuesday, July 21, 2020 5:50 AM
    Thursday, July 16, 2020 5:05 AM
  • Hi,

    You could use the SelectNodes method to select a list of nodes matching the XPath expression, like

    $xml.SelectNodes("//Location[Name='Dev Test 1']") | ForEach-Object {$_.ParentNode.RemoveChild($_)}


    You may refer to link below

    https://docs.microsoft.com/en-us/dotnet/standard/data/xml/select-nodes-using-xpath-navigation

    Best Regards,


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    • Edited by Ian Xue Friday, July 17, 2020 9:55 AM
    • Marked as answer by Capn Steve Tuesday, July 21, 2020 5:47 AM
    Friday, July 17, 2020 9:51 AM
  • Thanks, used a variation on this and the help from jrv to find the solution.

    $xml=[xml](Get-Content $Input)
    foreach ($c in $xml.Locations.Location)
    {
       If ($c.NAME -eq $remove)
         {
           $c.ParentNode.RemoveChild($c)
         }
    }

    Tuesday, July 21, 2020 5:49 AM
  • thanks so much, suggestions worked and now have a better understanding on XML and XPath.
    Tuesday, July 21, 2020 5:50 AM