Solved

Append node to XML file?

Posted on 2006-11-04
10
2,296 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
  • 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Title # Comments Views Activity
Problem to refer to value 8 78
Winform Module - What is the ASP.Net equiv 2 38
ASP Repeater control - onl;y show if flag is true 7 22
asp web application 3 35
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

685 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