Solved

Append node to XML file?

Posted on 2006-11-04
10
2,307 Views
Last Modified: 2013-11-19
How can I add the following node:
<user>
     <name> xxx </name>
     <id>1</id>
     <mark A="10" B="20" />
</user>


To the following XML file using XMLDocument or XMLwriterText:

<users>
     <user>
         <name> yyy</name>
          <id>1</id>
          <mark A="11" B="12" />
     </user>
     <user>
           <name> zzz</name>
           <id>2</id>
           <mark A="34" B="50" />
     </user>
</users>
0
Comment
Question by:Abdu_Allah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17873641
You could use something like:

            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\test.xml");

            XmlNode user = doc.CreateElement("user");
            user.AppendChild(doc.CreateElement("name")).InnerText = "xxx";
            user.AppendChild(doc.CreateElement("id")).InnerText = "1";

            XmlNode mark = doc.CreateElement("mark");
            mark.Attributes.SetNamedItem(doc.CreateAttribute("A")).InnerText = "10";
            mark.Attributes.SetNamedItem(doc.CreateAttribute("B")).InnerText = "20";

            user.AppendChild(mark);
            doc.FirstChild.AppendChild(user);

            doc.Save("C:\\test2.xml");
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17874504
Whay I got the following error:
System.InvalidOperationException: The current node cannot contain other nodes
Source Error:
Line 126:     doc.FirstChild.AppendChild(user)

 
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17874583
What does the XML you are appending to look like in full ? Is there a Schema or DTD associated with it ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17874714
No schema no DTD
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17874715
What does your full XML look like ?
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17874716
Here is the full file:

<?xml version="1.0" encoding="utf-8"?>
<users>
     <user>
         <name> yyy</name>
          <id>1</id>
          <mark A="11" B="12" />
     </user>
     <user>
           <name> zzz</name>
           <id>2</id>
           <mark A="34" B="50" />
     </user>
</users>
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17874717
Try it with just the snippet you posted before and see if that works.
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17874728
Same error.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 17874729
Ah, ok. Change the code to:

            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\test.xml");

            XmlNode user = doc.CreateElement("user");
            user.AppendChild(doc.CreateElement("name")).InnerText = "xxx";
            user.AppendChild(doc.CreateElement("id")).InnerText = "1";

            XmlNode mark = doc.CreateElement("mark");
            mark.Attributes.SetNamedItem(doc.CreateAttribute("A")).InnerText = "10";
            mark.Attributes.SetNamedItem(doc.CreateAttribute("B")).InnerText = "20";

            user.AppendChild(mark);
           
            XmlNode users = doc.SelectSingleNode("/users");
            users.AppendChild(user);

            doc.Save("C:\\test2.xml");
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17874760
Many thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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