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 8720131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:57:47+00:00 2026-06-13T06:57:47+00:00

I am trying to add an XML node to multiple parent nodes(which have same

  • 0

I am trying to add an XML node to multiple parent nodes(which have same name). But it is only adding to the Last node of the XML and not in all.

input XML

<Record>
 <Emp>
  <ID>12</ID>
  <Name>ABC</Name>
 </Emp>
 <Emp>
  <ID>12</ID>
  <Name>ABC</Name>
 </Emp>
</Record>

I want to add the Location element to every Emp node. My code is as below:

XmlNodeList xNodeList = doc.SelectNodes("/Record/Emp");
XmlElement xNewChild = doc.CreateElement("Location");
        xNewChild.InnerText = "USA";
        foreach (XmlNode item in xNodeList)
        {
            item.AppendChild(xNewChild);
        }
doc.Save(path);

but I am getting output like this:

 <Record>
 <Emp>
  <ID>12</ID>
  <Name>ABC</Name>
 </Emp>
 <Emp>
  <ID>12</ID>
  <Name>ABC</Name>
  <Location>USA</Location>
 </Emp>
</Record>

The Location element has not been added to the first Emp node.

Note: After debugging, I am able to find that the element has been added even for the first Emp node. But, in the saved XML file I am seeing this strange behavior.

  • 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-13T06:57:48+00:00Added an answer on June 13, 2026 at 6:57 am

    Your xNewChild is a single new element. Simply adding it to multiple nodes will only serialize to the last node. A change like this should work:

    XmlNodeList xNodeList = doc.SelectNodes("/Record/Emp");
    foreach (XmlNode item in xNodeList)
    {
      XmlElement xNewChild = doc.CreateElement("Location");
      xNewChild.InnerText = "USA";
      item.AppendChild(xNewChild);
    }
    doc.Save(path);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a root XML node called and I am trying to add a
I'm trying to add an Element into an already existing Node/Parent like so: <?xml
I'm trying to add a child to an XML-node by loading a string as
I'm just trying to add the people nodes from one XML to another with
I have below updateFile code, here I am trying to add new node when
I have an XML file I'm trying to modify and want to add a
In java xml file I am trying to add a element to connection node
I'm trying to add a processing instruction node to an existing XML document to
I am trying to programmatically add a new XLink node to an XDocument but
I'm trying to read an xml file into memory, add a node, then write

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.