Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

June 6, 2010

Writing to an XML File

Filed under: C#,VB.NET,XML @ 10:57 pm

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.

4 Comments

  1.   Rostov — June 7, 2010 @ 2:16 pm    Reply

    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”)
    )
    );
    http://msdn.microsoft.com/en-us/library/bb387019.aspx

  2.   Maryellen — June 30, 2011 @ 1:13 am    Reply

    Finllay! This is just what I was looking for.

  3.   ruel — March 18, 2012 @ 8:04 pm    Reply

    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

    TrackingNo="123"
    NoDays="45"
    AsOf="03-15-2012">



    All Document are OK

  4.   petehahn — July 30, 2012 @ 1:21 pm    Reply

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

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2014 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs