Solved

Append node to XML file?

Posted on 2006-11-04
10
2,316 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

627 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