Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Cannot add child nodes to existing XML document in C#

Posted on 2007-08-01
4
Medium Priority
?
1,001 Views
Last Modified: 2010-04-15
Question pertains to C#...

Hello,
I am trying to add an element (new_child) to an existing XML.

Existing file looks like this:

<root>
   <number>1</number>
   <number>2</number>
   <number>3</number>
<root/>

Am trying to get to this:
<root>
   <new_child>
      <number>1</number>
   </new_child>
   <new_child>
      <number>2</number>
   </new_child>
   <new_child>
      <number>3</number>
   </new_child>
<root/>

This is the code I tried:

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\numbers.xml");
//Get the root node
XmlNode rootNode = doc.SelectSingleNode("root");
// Create a new child node
XmlNode newChild = doc.CreateElement("new_child");
//add the child node to the root node
rootNode.AppendChild(newChild);
// Save the xml file
doc.Save(@"C:\new_numbers.xml");

This adds only the closing element of "new_child" to the existing file as one child element of "root" and "new_child" DOES NOT enclose each existing "number" element as desired.

Can anyone help please
0
Comment
Question by:Saxitalis
  • 2
4 Comments
 
LVL 6

Expert Comment

by:gjutras
ID: 19608720
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\numbers.xml");
//Get the root node
XmlNode rootNode = doc.SelectSingleNode("root");
// Create a new child node
XmlNode newChild = doc.CreateElement("new_child");
XmlNode newSubChild = doc.CreateElement("number");
newSubChild.InnerText = 1; // or 2 or 3
newChild.AppendChild(newSubChild);
//add the child node to the root node
rootNode.AppendChild(newChild);
// Save the xml file
doc.Save(@"C:\new_numbers.xml");
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 19608980
err.. you need 3 new_child nodes, and need to shift the number elements into them.. So you'll need a loop of some sort, won't you?

What's the overall possible structure? Can there be anything else other than 'root' and 'number' elements?
0
 
LVL 6

Accepted Solution

by:
gjutras earned 2000 total points
ID: 19610338
Ah, I see.  Will this do what you're trying to do.  Read the old one, then create a new document from scratch copying the values from the old one?

            XmlDocument doc = new XmlDocument();
            doc.Load(@"C:\numbers.xml");
            XmlDocument newdoc = new XmlDocument();
            //Get the root node
            XmlNode rootNode = doc.SelectSingleNode("root");
            XmlNode newRootNode = newdoc.CreateElement("root");
            newdoc.AppendChild(newRootNode);
            foreach (XmlNode node in rootNode.ChildNodes)
            {
                XmlNode newChild = newdoc.CreateElement("new_child");
                XmlNode newSubChild = newdoc.CreateElement("number");
                newSubChild.InnerText = node.InnerText;
                newChild.AppendChild(newSubChild);
                newRootNode.AppendChild(newChild);
            }
            // Save the xml file
            newdoc.Save(@"C:\new_numbers.xml");
0
 

Author Comment

by:Saxitalis
ID: 19614215
That is it!!! Thank you very much:)

Saxitalis
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question