473,386 Members | 1,647 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,386 software developers and data experts.

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">Content1</resource>
<resource name="222">Content2</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">Content1</resource>
<resource name="222">Content2</resource>
</CCC>

<DDD>
<resource name="333">Content3</resource>
<resource name="444">Content4</resource>
</DDD>

</BBB>
</AAA>
////////////////////////////////////////////////////
Nov 15 '08 #1
2 9242
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">Content1</resource>
<resource name="222">Content2</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">Content1</resource>
<resource name="222">Content2</resource>
</CCC>

<DDD>
<resource name="333">Content3</resource>
<resource name="444">Content4</resource>
</DDD>

</BBB>
</AAA>
Load the original XML document into a System.Xml.XmlDocument, 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.CreateElement("DDD")

Dim resource As XmlElement = doc.CreateElement("resource")
resource.SetAttribute("name", "333")
resource.InnerText = "Content3"
ddd.AppendChild(resource)

resource = doc.CreateElement("resource")
resource.SetAttribute("name", "444")
resource.InnerText = "Content4"
ddd.AppendChild(resource)

doc.DocumentElement("BBB").AppendChild(ddd)

'Save to Console for testing, could of course save to file
doc.Save(Console.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*******@yahoo.de>
??????:eh*************@TK2MSFTNGP06.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">Content1</resource>
<resource name="222">Content2</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">Content1</resource>
<resource name="222">Content2</resource>
</CCC>

<DDD>
<resource name="333">Content3</resource>
<resource name="444">Content4</resource>
</DDD>

</BBB>
</AAA>

Load the original XML document into a System.Xml.XmlDocument, 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.CreateElement("DDD")

Dim resource As XmlElement = doc.CreateElement("resource")
resource.SetAttribute("name", "333")
resource.InnerText = "Content3"
ddd.AppendChild(resource)

resource = doc.CreateElement("resource")
resource.SetAttribute("name", "444")
resource.InnerText = "Content4"
ddd.AppendChild(resource)

doc.DocumentElement("BBB").AppendChild(ddd)

'Save to Console for testing, could of course save to file
doc.Save(Console.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
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...
1
by: novadoft | last post by:
var textTobeWritten = blah(); var fso = new ActiveXObject("Scripting.FileSystemObject"); var tempFile = fso.OpenTextFile(originalFilename, 2, true); tempFile.Write(textTobeWritten);...
1
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...
6
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...
0
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...
3
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
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...
3
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...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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.