473,802 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Write or append the Element to XML file using XmlDocument

yxq
I want to add some elements to a XML file using XmlDocument, how to do?
thank you very much.
The original XML file like:
////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<AAA>
<BBB>
<CCC>
<resource name="111">Cont ent1</resource>
<resource name="222">Cont ent2</resource>
</CCC>
</BBB>
</AAA>
///////////////////////////////////////////////////////

After added elements(the "DDD" elements), it sould like:
////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<AAA>
<BBB>
<CCC>
<resource name="111">Cont ent1</resource>
<resource name="222">Cont ent2</resource>
</CCC>

<DDD>
<resource name="333">Cont ent3</resource>
<resource name="444">Cont ent4</resource>
</DDD>

</BBB>
</AAA>
////////////////////////////////////////////////////
Nov 15 '08 #1
2 9290
yxq wrote:
I want to add some elements to a XML file using XmlDocument, how to do?
thank you very much.
The original XML file like:
////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<AAA>
<BBB>
<CCC>
<resource name="111">Cont ent1</resource>
<resource name="222">Cont ent2</resource>
</CCC>
</BBB>
</AAA>
///////////////////////////////////////////////////////

After added elements(the "DDD" elements), it sould like:
////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<AAA>
<BBB>
<CCC>
<resource name="111">Cont ent1</resource>
<resource name="222">Cont ent2</resource>
</CCC>

<DDD>
<resource name="333">Cont ent3</resource>
<resource name="444">Cont ent4</resource>
</DDD>

</BBB>
</AAA>
Load the original XML document into a System.Xml.XmlD ocument, create and
insert the new elements and save the XmlDocument back e.g.

Dim doc As New XmlDocument()
doc.Load("..\.. \XMLFile1.xml")

Dim ddd As XmlElement = doc.CreateEleme nt("DDD")

Dim resource As XmlElement = doc.CreateEleme nt("resource")
resource.SetAtt ribute("name", "333")
resource.InnerT ext = "Content3"
ddd.AppendChild (resource)

resource = doc.CreateEleme nt("resource")
resource.SetAtt ribute("name", "444")
resource.InnerT ext = "Content4"
ddd.AppendChild (resource)

doc.DocumentEle ment("BBB").App endChild(ddd)

'Save to Console for testing, could of course save to file
doc.Save(Consol e.Out)

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 15 '08 #2
yxq
Thank you very much, you are a expert.

"Martin Honnen" <ma*******@yaho o.de>
??????:eh****** *******@TK2MSFT NGP06.phx.gbl.. .
yxq wrote:
>I want to add some elements to a XML file using XmlDocument, how to do?
thank you very much.
The original XML file like:
////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<AAA>
<BBB>
<CCC>
<resource name="111">Cont ent1</resource>
<resource name="222">Cont ent2</resource>
</CCC>
</BBB>
</AAA>
///////////////////////////////////////////////////////

After added elements(the "DDD" elements), it sould like:
////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<AAA>
<BBB>
<CCC>
<resource name="111">Cont ent1</resource>
<resource name="222">Cont ent2</resource>
</CCC>

<DDD>
<resource name="333">Cont ent3</resource>
<resource name="444">Cont ent4</resource>
</DDD>

</BBB>
</AAA>

Load the original XML document into a System.Xml.XmlD ocument, create and
insert the new elements and save the XmlDocument back e.g.

Dim doc As New XmlDocument()
doc.Load("..\.. \XMLFile1.xml")

Dim ddd As XmlElement = doc.CreateEleme nt("DDD")

Dim resource As XmlElement = doc.CreateEleme nt("resource")
resource.SetAtt ribute("name", "333")
resource.InnerT ext = "Content3"
ddd.AppendChild (resource)

resource = doc.CreateEleme nt("resource")
resource.SetAtt ribute("name", "444")
resource.InnerT ext = "Content4"
ddd.AppendChild (resource)

doc.DocumentEle ment("BBB").App endChild(ddd)

'Save to Console for testing, could of course save to file
doc.Save(Consol e.Out)

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 16 '08 #3

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

Similar topics

1
2124
by: NagaKiran | last post by:
Hi I am able to display XML contents in a treeview. But I am unable to create an XML file using treeview data. I have a TreeView control wich contains many levels of data.I want create write an XML file using the data of treeview. How can this be done...
1
1977
by: novadoft | last post by:
var textTobeWritten = blah(); var fso = new ActiveXObject("Scripting.FileSystemObject"); var tempFile = fso.OpenTextFile(originalFilename, 2, true); tempFile.Write(textTobeWritten); tempFile.close(); alert("done!! "); --------------------
1
2690
by: SirPoonga | last post by:
I am storing some small amounts of data in an xml file using XmlDocument. I basically do a .Load(filename), processing, then .Save(filename). However, once and awhile on the .Load it will say the file is in use by another process. This is the only program to use that file. I am using C#. Any ideas on what is going on?
6
9792
by: 000dreamsound000 | last post by:
Hi I need help with to access a text file over my local network. I need to be able to create,read, write and delete the txt file on a specific machine(s) from any computer over the network. The folder where the txt file should be created is enabled for sharing and allows other users on the network to change files (XP Pro on all machines). My code is as follows: // Writing Local IP to File fstream myfile; myfile.open...
0
1252
by: newUser1234 | last post by:
I'm trying to read and write from an excel file using this method..My application can read the excel file without problems but doesn't write anything to the file i want it to write. I've also tried adding a '.Value' to the end, it doesn't seem to help. I have closed the file in the end as well.. Any Suggestions would be very helpful Dim excel_app As New Excel.Application Dim xlWkBk As Excel.Workbook Dim xlSheet As...
3
1740
by: neehakale | last post by:
can ny body tel me how to write a data from form say, Name and Age to excel file...
2
3213
by: sajidali | last post by:
i am trying to append a text file using c#. when i executes my application i writes to the text file using more then one functions. i want to append file only during execution of programe. next time when i execute programe it should overwrite the file. plz help me in this regard.
3
7084
BishoMicho
by: BishoMicho | last post by:
What i want to do is to implement visitor counter in the home page of a web application, with daily visitors and total visitors. i don't want to use code behind, i want to use javascript to write in local file (on server) not on the client side the current number of visitors Can any one help. Best Regads.
1
2231
by: Arulmanoj | last post by:
Hi, I have to write the content from one csv file to another csv file using VB Script. The scource content file contains 5 columns and the destination file contains six column as below. Source File: Coulmn1 Coulmn3 Coulmn4 Coulmn5 Coulmn6 abc abc abc abc abc Destination File: Coulmn1 Coulmn3 Coulmn3 Coulmn4 Coulmn5 Coulmn6
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.