Writing to an XML File

Posted Sun, Jun 6 2010 20:57 by Deborah Kurata

Sometimes, you just need to write some values to an XML file. If you are targeting the .NET Framework 3.5 or higher, you can use XDocument and XElement to do this easily.

This post demonstrates how to write values to an XML string. The example writes the values from three TextBoxes, but you can use this technique to write any information to an XML file.

In C#:

var doc = new XDocument();

var emp = new XElement("Employee");

emp.Add(new XElement("LastName", textBox1.Text));
emp.Add(new XElement("FirstName", textBox2.Text));
emp.Add(new XElement("PhoneNumber", textBox3.Text));

doc.Add(emp);

doc.Save("Employee.xml");

In VB:

Dim doc = New XDocument()

Dim emp = New XElement("Employee")

emp.Add(New XElement("LastName", TextBox1.Text))
emp.Add(New XElement("FirstName", TextBox2.Text))
emp.Add(New XElement("PhoneNumber", TextBox3.Text))

doc.Add(emp)

doc.Save("Employee.xml")

In VB using XML Literals:

Dim doc = New XDocument()

Dim emp = <Employee>
              <LastName><%= TextBox1.Text %></LastName>
              <FirstName><%= TextBox2.Text %></FirstName>
              <PhoneNumber><%= TextBox3.Text %></PhoneNumber>
          </Employee>
doc.Add(emp)

doc.Save("Employee.xml")

This code first creates a new XDocument that defines the XML document.

In the first two examples, the code then creates a new XElement defining the root node of the XML string. In this case, the root node is "Employee", but it can be anything.

The sub elements are then added to the Employee root element. In this case, the sub elements are "LastName", "FirstName", and "PhoneNumber" and the values of these elements come from the TextBoxes.

The third example above uses XML literals to perform the same operation. It builds the XML string using the <%= %> replacement syntax to populate the value of each element.

In each example, the new Employee element is then added to the XML document and saved.

The resulting XML file is shown below.

XML File:

<?xml version="1.0" encoding="utf-8"?>
<Employee>
  <LastName>Baggins</LastName>
  <FirstName>Bilbo</FirstName>
  <PhoneNumber>(925)555-1234</PhoneNumber>
</Employee>

(See this post for information on reading the above XML file.)

Use one of the above techniques any time you need to write values to an XML file.

Enjoy!

EDIT 6/7/10: To write XML documents that include attributes, see this other post.

Filed under: , , , ,

Comments

# Reading an XML File

Sunday, June 06, 2010 11:23 PM by Deborah's Developer MindScape

This prior post detailed how to write to an XML file. This post details how to read that XML file. The

# Interesting Finds: June 7, 2010

Monday, June 07, 2010 5:19 AM by Jason Haley

Interesting Finds: June 7, 2010

# re: Writing to an XML File

Monday, June 07, 2010 2:16 PM by Rostov

Again just for the sake of some 'tips' if anyone wants to play further:

You don't "need" to use an XDocument to start things off if you are going to use the LINQX stuff to do your work in terms of loading/working/saving.  The points to note are that calling a 'ToString' operation on such a document does not return the 'document declaration' components, but if you call .Save on an XElement it creates those for you.

The second thing is that it is sometimes nice for readability to use the functional construction of your elements so that the code resembles the actual construction of the xml document:

(C#)

XElement emp =

new XElement("employee",

new XElement("lastname", "Rostov"),

new XElement("firstname", "Mikhail"),

new XElement("phonenumber",

new XAttribute("extension", "22938"),

new XElement("areacode", "555"),

new XElement("exchange", "451")

)

);

msdn.microsoft.com/.../bb387019.aspx

# Writing to an XML File With Attributes

Monday, June 07, 2010 10:57 PM by Deborah's Developer MindScape

This prior post demonstrated a very simple technique for writing to an XML file that has a set of elements

# re: Writing to an XML File

Thursday, June 30, 2011 1:13 AM by Maryellen

Finllay! This is just what I was looking for.

# reading xml

Sunday, March 18, 2012 8:04 PM by ruel

im new to xml and visual basic 2010

how ca i read this xml file cause mot samle i see

is single elements how about this kind of xml

Thanks in advance

<RESPONSE

ISCorrect="YES"

TrackingNo="123"

NoDays="45"

AsOf="03-15-2012">

<Member SexIS="M" LASTNAME="bruce" FIRSTNAME="Wayne" MIDDLENAME="A" Character="Batman" BIRTHDATE="02-09-1967"/>

<LogIn ADMITTED="03-15-2012" DISCHARGE="03-15-2012"/>

<DOCUMENTS>

<DOCUMENT DocNumber="204" DocName="Birth Certificate" >All Document are OK </DOCUMENT>

</DOCUMENTS>

</RESPONSE>

# re: Writing to an XML File

Monday, July 30, 2012 1:21 PM by petehahn

how would you write that same element to a specific location in an existing xml document (i.e. you wanted to add employee bilbo in the node "shire" as opposed to "mordor")

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: