1. Home
  2. Computing & Technology
  3. Visual Basic

Coding an XML Configuration File
The code . . .

By Dan Mabbutt, About.com

In my opinion, however, if you want your program to be as easy to understand as possible, and you want the data itself to be easy-to-read text too, then creating a configuration file using XML methods is probably the way to go. Most modern programming technologies now completely support XML and you can open an XML file in either Notepad or most browsers to see what's in it. Writing the file directly with XML methods in VB.NET makes what is happening as clear as possible in the program code. The alternative of "serializing" to an XML file does a lot of the work for you but also makes the creation of the XML a "black box". The example you will see here puts it all out there in the open.

As an example of an alternative, here's a program designed to be used as a desktop utility for simple calculations of stock market prices and amounts. The starting values are set using an XML configuration file. The starting values that will be used the next time the program is loaded can be set using an update routine that overwrites the XML file with new values. The illustration below shows the XML file displayed in IE. (IE is one of the best XML utilities!)

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The first task for the program is to write the XML file. The code that writes the XML file is shown below.

  Dim Doc As New XmlDocument
  'Create a 'vanilla' XML Declaration (processing instruction)
  Dim myXmlDeclaration As XmlDeclaration
  myXmlDeclaration = _
    Doc.CreateXmlDeclaration("1.0", "UTF-8", "yes")
  Doc.AppendChild(myXmlDeclaration)
  '       <-Root Created
  Dim Root As XmlElement
  Root = Doc.CreateElement("StockCalcDefaults")
  'Create a company name child node
  Dim Child As XmlElement
  Child = Doc.CreateElement("companyName")
  Child.InnerText = companyName.Text
  Root.AppendChild(Child)
  'Create a Buy Price child node
  Child = Doc.CreateElement("buyPrice")
  Child.InnerText = _
    CStr(Decimal.Round(buyPrice.Value, 2))
  Root.AppendChild(Child)
  'Create a Sell Price child node
  Child = Doc.CreateElement("sellPrice")
  Child.InnerText = _
    CStr(Decimal.Round(sellPrice.Value, 2))
  Root.AppendChild(Child)
  'Create a Number of Shares child node
  Child = Doc.CreateElement("numShares")
  Child.InnerText = CStr(numShares.Value)
  Root.AppendChild(Child)
  'Add an attribute to the child node
  Dim Attr As XmlAttribute
  Attr = Doc.CreateAttribute("incByThousand")
  Attr.Value = CStr(incByThousand.Checked)
  Child.Attributes.Append(Attr)
  'Create a Profit Amount child node
  Child = Doc.CreateElement("profitAmount")
  Child.InnerText = _
    CStr(Decimal.Round(profitAmount.Value, 2))
  Root.AppendChild(Child)
  '       <-Root Added to Doc
  Doc.AppendChild(Root)
  'Write XML to a file
  Dim Output As New XmlTextWriter( _
    "StockDefaults.xml", _
    System.Text.Encoding.UTF8)
  '       <-Document Written
  Doc.WriteTo(Output)
  Output.Close()

The explanation of the code - and the download link - for the program is on the next page.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Coding an XML Configuration File

©2009 About.com, a part of The New York Times Company.

All rights reserved.