Convert a file with a special format to an XML file

Source: Internet
Author: User
Tags readline split
XML If we now have such a file
The contents are as follows:
Room_number Breakfast Lunch Dinner
290 Bagel Pizza Salmon
Chicken Orange Pizza ala King
349 Sweet Roll Salad Tofu and vegetables
Omelet Sausage Veal
702 Eggs Tuna Fish Cheese Sandwich
Each item in the file is split with vbtab
So now we're going to automatically translate it into an XML file like the following
<kitchen xmlns= "The_roach_motel" >
<dinner>chicken Ala king</dinner>
<breakfast>sweet roll</breakfast>
<dinner>tofu and Vegetables</dinner>
<lunch>tuna fish</lunch>
<dinner>cheese sandwich</dinner>
What do we need to do:
We need to use StreamReader to read the contents of the file, store it in a temporary dataset, and then use the dataset's Getxml () to get the XML file
Let ' go
Imports System
Imports System.IO
Imports System.Collections
Imports System.Data
Imports System.Text
Module Modxml
Sub Main ()
Dim Strxml as String
Strxml = Delimiteddataset (VbTab, "C:/")
' You may need to make the necessary modifications
End Sub

Function Delimiteddataset (ByVal strdelimiter as String, _
ByVal strFilePath As String) as String
Dim ODS as New DataSet ()
Dim Strfields as String
Dim otable as New DataTable ()
Dim Orows as DataRow
Dim intcounter as Int32 = 0
Dim Orow as DataRow ()

Ods.datasetname = "Kitchen"
Ods.namespace = "The_roach_motel"
ODS.TABLES.ADD ("Room_service")

Dim OSR as New StreamReader (strFilePath)
' To the head of the file
OSR.BaseStream.Seek (0, Seekorigin.begin)
' Add to Header Columns
For each strfields in Osr.readline (). Split (Strdelimiter)
Ods.tables (0). Columns.Add (Strfields)

' Now add rows

otable = ods.tables (0)
while (Osr.peek () >-1)
Orows = Otable.newrow ()
For each strfields in Osr.readline (). Split (Strdelimiter)
Orows (intcounter) = Strfields
intcounter = intcounter + 1
intcounter = 0
End While
Return Ods.getxml ()
' Ods.writexml ("C:/food.xml")
' or write it to a hard disk.
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.