Updating Legacy Data to XML, with ASP.NET

Introduction

This tutorial discusses how to read various forms of legacy data, such as Comma Separated Values (CSV) or Tab Separated Values (TSV). It then goes on to cover both saving and displaying this data in Extensible Markup Language (XML), using the ASP.NET language, with Visual Basic .NET.

The Process

Starting with a CSV file, we are going to open and read our legacy data into a dataset, using a loop to read each row in turn. We'll then output this data into an XML file, with our chosen markup, again using a loop to write each element. Writing this data back to the XML file will be similar to writing plain text lines, as Visual Basic .NET has some great built-in methods for using XML.

The Legacy Data

In the case of a CSV file, values are divded into columns using commas. Using ASP.NET with VB.NET, it is easy to read this text file into a dataset or array, with each column value being added to a column.

Code coming soon.

 

The XML

 In a new website project, open the Default.aspx file and switch to design view, then double click on the empty page - this will load the code behind file, and create a sub statement for the page_load event. Code entered here will be executed when the page is loaded in a web browser.

At the very top of this code behind file, create a couple of line spaces, then on line one type:

Imports System.Xml

By default the .NET Framework includes the most common modules, but in order to use more advanced features of XML we need to import this one. This will allow us to use the XmlTextWriter, to quickly create an appropriately formatted XML file. This can be used as shown below:

Dim objXML As New XmlTextWriter("D:/NewXML.xml", Encoding.UTF8)
objXML.WriteStartElement("book")
objXML.WriteAttributeString("title", "A Book Title")
objXML.WriteRaw(Environment.NewLine)
objXML.WriteElementString("link", "http://www.nickjohnston.co.uk/?ISBN=455332")
objXML.WriteRaw(Environment.NewLine)
objXML.WriteElementString("isbn", "
455332")
objXML.WriteRaw(Environment.NewLine)
objXML.WriteElementString("publish_date", "04/12/07")
objXML.WriteRaw(Environment.NewLine)
objXML.WriteEndElement()
objXML.Flush()
objXML.Close()

In order to create an XML file with a friendly layout, this code also adds line breaks, using WriteRaw(Environment.NewLine).

The above code will create an XML document, with the following structure.

<book title="A Book Title">
<link>http://www.nickjohnston.co.uk/?ISBN=455332</link>
<isbn>455332</isbn>
<publish_date>04/12/07</publish_date>
</book>

The code used here can easily be looped in order to write multiple elements to an XML file.

Updating Legacy Data to XML, with ASP.NET was originally written on 28/10/2008 18:23:35, and last updated on the 05/11/2008 02:43:54 category.

Comment on Updating Legacy Data to XML, with ASP.NET.

About the author

Nick Johnston is a self employed computing consultant, working in several facets of the IT industry. With an initial interest in business and computer security, his roles have spread to many other areas.

Nick is currently available for project and contract work - contact Nick for more information.


Web development | Nick Johnston