Often times people want to write out information to a file instead of a database. Problem is xml is hard to understand at times and code samples aren’t much better. I am going to do 3 or 4 posts on how to do basic interaction with an XML file.
Usually the most important part about using an XML file is first writing to it, but how can you write to it if you don’ t have an XML file? Below is a basic XML file I have come up with that we are going to read, write and delete from along with an explanation of the XML file.
The XML File
The concept behind this xml file is first you have the root node, everthing <> is pretty much refered to a node or element (we will use node). The root node holds EVERYTHING in the xml file and can be your starting point if need be. Next is the contacts node that will hold each individual contact and their information.
Next we get to the meat of the xml file. The contact node. There will be, in the case of this xml file, multiple contact nodes with “Child” nodes. Basically, the contact node will have nodes which contain all the information. I look at xml files kind of like the earth. You have your innercore which is you main information, but it needs to be surrounded by the outercore. Then you have the mantel which surrounds the outercore followed by the crust of the earth, everything is layered. Same with XML I have it pictured in my head in layers and everything needs to be accessed in those layers in order.
First, lets look at the constructor of our first class. I usually just create a class that handles all the data read, write, and delete stuff. Lets call it DataAccess.cs. At this point create a basic console project and add a DataAccess.cs class to it, and an xml file with the above xml. Here is the constructor I use to start to start with the xml processing. (note: localpath is cut off because it is long)
In the above code we have the private datamembers and the constructor. I’ll explain the code line by line.
private string _path;
Here we have a string that is going to be use for our full path to the xml file. When I say full path I mean name of the file and the location on the harddrive. So we could potentially have C:\folder\file.xml
private string _localPath = @”The_Path_to_the_project_folder_with_xml_file_”;
In this line we are hardcoding the path to the folder where the xml file resides. Doing it this way in real world applications is bad instead use a configuration file, like an xml file, to get the information and set where the xml file is. Hardcoding paths is bad.
For this snippet of code you should have a “using System.Xml;” statement in your using directives. What this code is doing is setting up an object that is going to hold our xml information so that we can access that information. In the constructor is where we are going to add all the xml to this object.
public DataAccess(string xmlDocumentName)
This is the constructor that will take in the name of the xml file we want to use. So in the case of our xml file it will be “Contacts.xml”.
_path = _localPath + xmlDocumentName;
Here we are populating the full path of the xmlfile with the absolute location and the xml’s filename. It might look like C:\XMLProject\bin\debug\Contact.xml if you were to write out what it was.
doc = new XmlDocument();
Here we are initializing this object and allowing it to start being used by the rest of the code.
And this is our magical line. This takes that xml file that we got the name and location of grabs all of the xml from the file and pops it into the XmlDocument object named doc. With that we are ready to start messing with the XML.
Code to write to an XML file
Alright the fun part as arrived actually writing your data out to the xml file. It is actually a fairly simple thing to do once you understand what is going on so lets get started.