Part 1: Write to XML File Using C#
Part 2: Reading Data from XML File Using C#
Part 3: Delete Data from XML File Using C#
In the last post, Write to XML File Using C#, I talked about our XML document and how to write to an xml document. Doing so is fine and dandy, but useless if we can't read it. If you want to know about the xml file and some of the information surrounding it please visit the Part 1: Write to XML File Using C# to find out more. We are going to just jump right on in to the code.
The Code
Lets step through this code.
public List<Contact> RetrieveContacts()
This line is our method declaration we are basically going to end up with a list of contacts to give for our application to use from our XML file.
List<Contact> list = new List<Contact>();
This is the list object we are creating and going to return back at the end of the method. An easy very simplified way to look at a List<> object is as an array that holds a bunch of other objects of whatever type you need it to be. I just basically think of them as an array that I can use; there is more to it, but I'm not advanced enough to reap the benefits.
XmlNode root = doc.SelectSingleNode("//contacts");
This is getting the top level node of the xml where all the contacts are below it. So that in the next wine we can grab all the contacts.
XmlNodeList nodeList = root.SelectNodes("contact");
I like this line of code it is kind of fun in what it does. In this case we are creating a list to get a collection of objects. Or this it grabs all the contact nodes and puts them in an object for us to get the information out of.
foreach (XmlNode n in nodeList)
The foreach loop is one of my favorite loops because once I "grok'd it" it was a lot of fun to start using. What the foreach is doing is saying hey I have an collection here with a bunch of objects in it. I want to go to every single object in it until I get to the end and perform some operation. So in the case of this one it sees that there is an object with a bunch of contact nodes in it. So it is going to go to each contact node and do something.
Contact c = new Contact();
We are creating a contact object because that is what our list object takes that we are going to return at the end of the method. From here we are going to populate the information in the contact object and add it to our list object.
c.GUID = n.SelectSingleNode("Guid").InnerText;
c.Name = n.SelectSingleNode("Name").InnerText;
c.Email = n.SelectSingleNode("Email").InnerText;
c.PhoneNumber = n.SelectSingleNode("PhoneNumber").InnerText;
With these lines we re taking the contact object we just made and adding the information from the xml file, but going into the contact node and selecting a specific node and grabbing the text between the <></>
list.Add(c);
This is the very last line of our foreach loop. It takes that contact object we created and adds it to our main list object.
return list;
Finally at the last line of the method. Here we return that list object to the main part of the application for it to be used.
Wrap-Up
Alright lets go through a paragraph explaining it top to bottom.
First, we created the method which in the end is going to be a list object that holds a bunch of contact objects for our application to use. We proceed through the method first creating our main list object. We then proceed to get the top upper level node out of the xml file that holds all of our contact nodes. Once that is done we take that information and get all the contact nodes and put them in to NodeList object to be used. We then make a contact object fill in the relevent properties from our contact nodes and add it to our main list object. From there we return the list and now our method call is a list object full of contact objects for us to process.
This wraps-up basic reading from an xml file. Only thing really left is deleting data from an xml file.