Reformatting Files
Let's suppose that you have a list of states in a file called c:\myData\states-abbreviations.txt in the following format:
AL ALABAMA
AK ALASKA
AZ ARIZONA
AR ARKANSAS
CA CALIFORNIA
...
Instead of this format, you need an XML file as shown below:
<?xml version="1.0" encoding="utf-8"?>
<FILE>
<AL>ALABAMA</AL>
<AK>ALASKA</AK>
<AZ>ARIZONA</AZ>
<AR>ARKANSAS</AR>
<CA>CALIFORNIA</CA>
...
</FILE>
(Note: XML files use tags similar to HTML. Don't worry if you do not know XML, just understand that the goal is to change from the first format to the second format.)
We will open c:\myData\states-abbreviations.txt as file 1, and open c:\myData\states-abbreviations.xml as file 2. We will read a line from the input file, parse it and write a line in XML format to the output file.
In additions, the output file will have the two lines at the start and end of the file.
Although the complete list of states is not shown here, bear in mind that there are some states, like New York and West Virgina that have a space in the name!
Notice that the program includes an openFile dialog control.
It uses FileExists to check if hte file already exists, then uses a message box to confirm that the user wants to replace an existing file.
Code: text-to-xml
End of lesson, Next lesson: