将具有特殊格式的文件转化为xml文件
发表于:2007-06-30来源:作者:点击数:
标签:
假如我们现在有这样的一个文件food.tab 内容如下: Room_NumberBreakfastLunchDinner 290BagelPizzaSalmon 301OrangePizzaChicken ala King 349Sweet RollSaladTofu and Vegetables 500OmeletSausageVeal 702EggsTuna fishCheese Sandwich 文件中每一项都是用
假如我们现在有这样的一个文件food.tab
内容如下:
Room_Number Breakfast Lunch Dinner
290 Bagel Pizza S
almon
301 Orange Pizza Chicken ala King
349 Sweet Roll Salad Tofu and Vegetables
500 Omelet Sausage Veal
702 Eggs Tuna fish Cheese Sandwich
文件中每一项都是用
vbtab进行分割的
那么现在我们要把它自动转化为一个如下的xml文件
<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>
我们需要怎么做呢:
我们需要利用StreamReader来读取文件内容,存放到一个临时的dataset中,最后用dataset的getxml()来得到这个xml文件
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")
@#你可能需要进行必要的修改
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)
@#到文件的头
oSR.BaseStream.Seek(0, SeekOrigin.Begin)
@#添加到 Header Columns
For Each strFields In oSR.ReadLine().Split(strDelimiter)
oDS.Tables(0).Columns.Add(strFields)
Next
@#现在添加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")
@#或者将它写到硬盘上
End Function
原文转自:http://www.ltesting.net