473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Insert node

Hi all,

Let say I have the following XML file:

<Books>
<book id = "1">
<title>ASP.NET</title>
<isbn>121212</isbn>
<price>32.65</price>
</book>
<book id = "2">
<title>Visual C#</title>
<isbn>4433434</isbn>
<price>56.32</price>
</book>
<book id = "3">
<title>ADO.NET</title>
<isbn>43434</isbn>
<price>55.65<.price>
</book>
</Books>
Now, how can I do the following by using C#:

How to insert a new element with child nodes as shown below under
<Books>...</Books>? :
<book id = "4">
<title>XML Bible</title>
<isbn>77676</isbn>
<price>85.65</price>
</book>
Thank you,

Your help is very very much appreciated.

Aug 2 '06 #1
3 6857
Depends on the quantity; if the xml is not immense, the XmlDocument will do
the job:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlElement root = doc.DocumentElement, newBook =
doc.CreateElement("book");
newBook.SetAttribute("id", "4");
newBook.AppendChild(doc.CreateElement("title")).In nerText = "XML
Bible";
newBook.AppendChild(doc.CreateElement("isbn")).Inn erText = "77676";
newBook.AppendChild(doc.CreateElement("price")).In nerText = "85.65";
root.InsertAfter(newBook, root.LastChild);
Debug.WriteLine(doc.OuterXml);

Marc
Aug 2 '06 #2
Thanks a lot, that works fine for me

Marc Gravell wrote:
Depends on the quantity; if the xml is not immense, the XmlDocument will do
the job:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlElement root = doc.DocumentElement, newBook =
doc.CreateElement("book");
newBook.SetAttribute("id", "4");
newBook.AppendChild(doc.CreateElement("title")).In nerText = "XML
Bible";
newBook.AppendChild(doc.CreateElement("isbn")).Inn erText = "77676";
newBook.AppendChild(doc.CreateElement("price")).In nerText = "85.65";
root.InsertAfter(newBook, root.LastChild);
Debug.WriteLine(doc.OuterXml);

Marc
Aug 2 '06 #3
Here an example based on your xml:

XmlDocument xmlBook = new XmlDocument();

xmlBook.Load(@"books.xml");

XmlDocument xmlBookToInsert = new XmlDocument();

xmlBookToInsert.Load(@"books_to_insert.xml");

XmlNode nodeToImport = xmlBookToInsert.SelectSingleNode("/book");

XmlNode node = xmlBook.ImportNode(nodeToImport, true);

xmlBook.DocumentElement.AppendChild(node);

<da*******@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
Hi all,

Let say I have the following XML file:

<Books>
<book id = "1">
<title>ASP.NET</title>
<isbn>121212</isbn>
<price>32.65</price>
</book>
<book id = "2">
<title>Visual C#</title>
<isbn>4433434</isbn>
<price>56.32</price>
</book>
<book id = "3">
<title>ADO.NET</title>
<isbn>43434</isbn>
<price>55.65<.price>
</book>
</Books>
Now, how can I do the following by using C#:

How to insert a new element with child nodes as shown below under
<Books>...</Books>? :
<book id = "4">
<title>XML Bible</title>
<isbn>77676</isbn>
<price>85.65</price>
</book>
Thank you,

Your help is very very much appreciated.

Aug 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony B | last post by:
Hi I need to insert a new node into an exisiting XML file using VBScript. The file has the following structure: <TRANSACTION> <ORDER> <BILLING>... </BILLING> <ORDER_TOTALS>... </ORDER_TOTALS>...
4
by: mike | last post by:
Hello. Let's say I have the following XML document: <ROOT><Element1>Some Text</Element1></ROOT> I want to add a new element after Element1. I tried the following but, naturally, it doesn't...
10
by: florian | last post by:
Hi, we have a contention problem because of an application which tries to insert a duplicate row in a table with primary key. This insert fails of course but the locks are not released within...
3
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** it's been a while since i have poseted to this newsgroup, but for a long time i was not programming at all. but now that i am out of...
2
by: Patrick | last post by:
Hi, im trying to first insert a node in the xml file and save it. XmlDocument mDoc = new XmlDocument(); mDoc.Load("struct.xml"); XmlNode singleNode; XmlNode newNode =...
2
by: wombat53 | last post by:
Hi Group Are there any DB2 UDB ESE DPF V8.2 users exploiting "buffered inserts" (BIND parm INSERT BUF) *and* "multi-row INSERTS" (many rows associated with the VALUES clause of the INSERT to...
4
by: mathon | last post by:
Hello, im currently implementing a binary search tree means, that a greater number than root will be added as right child and a less number as left child. My insert function looks currently like...
6
by: askmatlab | last post by:
Hello all: I would like to insert a number into a linked list in ascending order. Is the following function correct? void insert(Node **node, int v) { Node *tmp = (Node...
7
by: E.F | last post by:
Hi everybody, I get lost trying to insert a node in my xml file with readfile, xpath, xpathnavigator, etc... 1/ my xml file looks like that : <PUPILL_CP_1> <YEAR_2006> <OCTOBRE...
10
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.