Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

June 6, 2010

Reading an XML File

Filed under: C#,VB.NET,XML @ 11:23 pm

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

The example reads the values and populates three TextBoxes, but you can use this technique to read any information from an XML file.

XML File:

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

In C#:

var doc = XDocument.Load("Employee.xml");

var emp = doc.Descendants("Employee").FirstOrDefault();

textBox1.Text = emp.Element("LastName").Value;
textBox2.Text = emp.Element("FirstName").Value;
textBox3.Text = emp.Element("PhoneNumber").Value;

In VB:

Dim doc = XDocument.Load("Employee.xml")

Dim emp = doc.Descendants("Employee").FirstOrDefault()

TextBox1.Text = emp.Element("LastName").Value
TextBox2.Text = emp.Element("FirstName").Value
TextBox3.Text = emp.Element("PhoneNumber").Value

In VB using XML Literals:

Dim doc = XDocument.Load("Employee.xml")

TextBox1.Text = doc…<LastName>.Value
TextBox2.Text = doc…<FirstName>.Value
TextBox3.Text = doc…<PhoneNumber>.Value

This code first loads the XML file into an XML document.

In the first two examples, the code finds the first Employee node. In this case, there is only one. It then uses the Element function to retrieve the defined element ("LastName", "FirstName", and "PhoneNumber") from the Employee node. The Value property provides the string value of the element. This value is assigned to the TextBox.

The third example above uses XML literals to retrieve the elements from the XML document. The "…" syntax denotes to search any descendants, so the Employee node does not have to be found first.

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

Enjoy!

4 Comments

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

    Just a word of caution:

    remember that using an XDocument for your loading starts your ‘search’ at the document declaration and not your actual root node of ‘data’.

    In this case because you’re using ‘descendants’ you’re fine — but if you weren’t, (and if you don’t need to use the document declaration for anything) using an XElement.Load works fine.

    And also be careful to note the distinction between ‘.Elements’ and ‘.Descendants’ — where the former returns the elements that match the current location/node you are on, and where Descendants matches any and ALL nodes with that name regardless of their nesting level into or below your current location.

  2.   DeborahK — June 7, 2010 @ 6:57 pm    Reply

    Thanks for the tips, Rostov!

  3.   Jeannie — June 30, 2011 @ 4:25 am    Reply

    Wham bam thank you, ma’am, my qeuisotns are answered!

  4.   Luke Pammant — September 7, 2012 @ 8:41 am    Reply

    Great tips! This is just what I was looking for. Short and to the point.

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