• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1145
  • Last Modified:

Cannot add child nodes to existing XML document in C#

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
Saxitalis
Asked:
Saxitalis
  • 2
1 Solution
 
gjutrasCommented:
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
 
andrewjbCommented:
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
 
gjutrasCommented:
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
 
SaxitalisAuthor Commented:
That is it!!! Thank you very much:)

Saxitalis
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now