Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:40:17+00:00 2026-06-04T14:40:17+00:00

PowerShell/xml beginner here…. I’m trying to append to or remove empty xml nodes using

  • 0

PowerShell/xml beginner here….
I’m trying to append to or remove empty xml nodes using PowerShell as part of a Nuget Package. The xml file has the following format…

<Root>
    <service name="first">
        <item>
        </item>
    </service>
    <service name ="second">
        <item>
        </item>
    </service>
</Root>

First my script select one of the services and save it as a variable, say if the user wants to select service 1…..

if ($xml.Root.service.name -eq $serviceName)
{
         $myService = $xml.Root.service
}

Problem is later on, I need to append elements to the node/delete the node…
I have something like

    $newNode = $xml.CreateElement('new'...
    .........

    $empty = $myService.SelectSingleNode('./item')
    $empty.PrependChild($newNode)

But I can’t get the this method to work.

Any suggestions would be appreciated…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:40:18+00:00Added an answer on June 4, 2026 at 2:40 pm

    This should help you out.

    # Get an XML document
    $MyXml = [xml]'<?xml version="1.0" encoding="utf-8"?><root><service name="foo"><item></item></service></root>';
    # Create a new element from the XmlDocument object
    $NewElement = $MyXml.CreateElement('new');
    # Select the element that we're going to append to
    $ServiceElement = Select-Xml -Xml $MyXml -XPath '/root/service[@name="foo"]/item';
    # Append the 'new' element to the 'item' element
    $ServiceElement.AppendChild($NewElement);
    # Echo the OuterXml property of the $MyXml variable to verify changes
    Write-Host -Object $MyXml.OuterXml;
    # Save the XML document
    $MyXml.Save('c:\test.xml');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to edit some xml in Powershell. Here's what I'm doing: $path =
I found a nice script to import xml using powershell http://slash4.de/tutorials/Automatic_mediawiki_page_import_powershell_script Currently I don't
I am trying to use powershell to convert a csv to an xml table.
I'm trying to use the powershell command select-xml to select data from an xml
As part of my package I need to add an xml file in a
I'd like to sort the following xml document using powershell. <car> <germany> <manufacturer>Opel</manufacturer> <manufacturer>BMW</manufacturer>
I'm trying to save an XML file from a powershell script to the standing
I am new to PowerShell, I am trying to change a property of an
I am working with powershell 1.0 using PSEXEC to run scripts remotely (or on
I'm trying to write a small Powershell function that will return some summary data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.