Solved

Append node to XML file?

Posted on 2006-11-04
10
2,240 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
Comment Utility
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
Comment Utility
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
Comment Utility
What does the XML you are appending to look like in full ? Is there a Schema or DTD associated with it ?
0
 
LVL 3

Author Comment

by:Abdu_Allah
Comment Utility
No schema no DTD
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
What does your full XML look like ?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Author Comment

by:Abdu_Allah
Comment Utility
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
Comment Utility
Try it with just the snippet you posted before and see if that works.
0
 
LVL 3

Author Comment

by:Abdu_Allah
Comment Utility
Same error.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
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
Comment Utility
Many thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
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.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now