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 Food.tab
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" >
<Room_Service>
<Room_Number>290</Room_Number>
<Breakfast>Bagel</Breakfast>
<Lunch>Pizza</Lunch>
<Dinner>Salmon</Dinner>
</Room_Service>
<Room_Service>
<Room_Number>301</Room_Number>
<Breakfast>Orange</Breakfast>
<Lunch>Pizza</Lunch>
<dinner>chicken Ala king</dinner>
</Room_Service>
<Room_Service>
<Room_Number>349</Room_Number>
<breakfast>sweet roll</breakfast>
<Lunch>Salad</Lunch>
<dinner>tofu and Vegetables</dinner>
</Room_Service>
<Room_Service>
<Room_Number>500</Room_Number>
<Breakfast>Omelet</Breakfast>
<Lunch>Sausage</Lunch>
<Dinner>Veal</Dinner>
</Room_Service>
<Room_Service>
<Room_Number>702</Room_Number>
<Breakfast>Eggs</Breakfast>
<lunch>tuna fish</lunch>
<dinner>cheese sandwich</dinner>
</Room_Service>
</Kitchen>
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
Code
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:/food.tab")
' 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)
Next

' 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
Next
intcounter = 0
OTABLE.ROWS.ADD (orows)
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: info-contact@alibabacloud.com 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.