473,326 Members | 2,175 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,326 software developers and data experts.

How to insert a new element in XML file?

Hi all,

Let say I have the following XML file:
<?xml version="1.0" ?>
<Store>

<cdCollect>
<cd id="1">
<title>Latest Hits</title>
<price>17.3</price>
</cd>
</cdCollect>

<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>

</Store>
Now, how can I do the following by using C#:

1. 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.

Regards,

Samir
Nov 16 '05 #1
3 11680
I am using "XmlNodeWriter", you can find it in gotdotnet.com

Here is how I use that.
XmlNodeWriter wALI = new XmlNodeWriter(docXml, false);

YourObject.Serialize(wALI, dtAliAddress);

J.W.

"Roshan" <km*****@vsnl.com> wrote in message
news:e5**************************@posting.google.c om...
Hi all,

Let say I have the following XML file:
<?xml version="1.0" ?>
<Store>

<cdCollect>
<cd id="1">
<title>Latest Hits</title>
<price>17.3</price>
</cd>
</cdCollect>

<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>

</Store>
Now, how can I do the following by using C#:

1. 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.

Regards,

Samir

Nov 16 '05 #2
XmlDocument doc = /* Load XML data. */ LoadDocument();
XmlNode parentNode = doc.DocumentElement; // Point at parent node of <book>.
XmlNode bookNode = doc.CreateNode(XmlNodeType.Element, "book", null);
doc.AppendChild(bookNode );
XmlAttribute attr = doc.CreateAttribute("id");
attr.InnerText = "4";
bookNode.Attributes.Append(attr);
XmlNode childNode = doc.CreateNode(XmlNodeType.Element, "title", null);
childNode.InnerText = "XML Bible";
bookNode.AppendChild(childNode);
// And further on..
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Roshan" <km*****@vsnl.com> wrote in message
news:e5**************************@posting.google.c om...
Hi all,

Let say I have the following XML file:
<?xml version="1.0" ?>
<Store>

<cdCollect>
<cd id="1">
<title>Latest Hits</title>
<price>17.3</price>
</cd>
</cdCollect>

<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>

</Store>
Now, how can I do the following by using C#:

1. 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.

Regards,

Samir

Nov 16 '05 #3
I don't know if this is related or not but if you are
into XML as a database, you can try to download the
XmlDbClient by Paul Wilson. (http://www.wilsondotnet.com)

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:<vd*******************@news4.e.nsc.no>...
XmlDocument doc = /* Load XML data. */ LoadDocument();
XmlNode parentNode = doc.DocumentElement; // Point at parent node of <book>.
XmlNode bookNode = doc.CreateNode(XmlNodeType.Element, "book", null);
doc.AppendChild(bookNode );
XmlAttribute attr = doc.CreateAttribute("id");
attr.InnerText = "4";
bookNode.Attributes.Append(attr);
XmlNode childNode = doc.CreateNode(XmlNodeType.Element, "title", null);
childNode.InnerText = "XML Bible";
bookNode.AppendChild(childNode);
// And further on..
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Roshan" <km*****@vsnl.com> wrote in message
news:e5**************************@posting.google.c om...
Hi all,

Let say I have the following XML file:
<?xml version="1.0" ?>
<Store>

<cdCollect>
<cd id="1">
<title>Latest Hits</title>
<price>17.3</price>
</cd>
</cdCollect>

<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>

</Store>
Now, how can I do the following by using C#:

1. 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.

Regards,

Samir

Nov 16 '05 #4

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

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
2
by: Brian | last post by:
I'm trying out a script called mktree.js < http://www.mattkruse.com/javascript/mktree/ > I want to add an expand all button to the script, using the DOM insertBefore method. I added a function to...
28
by: Giggle Girl | last post by:
Can someone show me how to insert a row at any given row index of an already created table? It only has to work in IE6 (used on intranet at work). Specifically, if a table is 20 rows in total...
7
by: Wojtek | last post by:
Hello, how can I transparently insert one XML file into another? I am looking for a method simmilar to the #include <file> known from the C language. To be more precise, an example: I have...
2
by: jkflens | last post by:
Hello, i convert one XML-document by using XSLT into another XML-document. First change all attributes to elements is no problem. Then i try to insert a new element into the new document by...
2
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python: 2.4 Example: <a> <b createAnotherWhenCondition="x"> <c>text for c</c> <d>text for d</d> </b>
0
by: shapper | last post by:
Hello, I am transforming a XML file into another XML file using a XSLT. Everything is working fine! I have only one problem: I want one of the values in my XSLT file to be taken from a Key...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
3
by: Begreen | last post by:
Hi All, I wrote a java program which outputs a xml file! But I would prefer this program to insert the DTD code on the fly, in the xml file when created! I want the xml file to look like...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.