假如我们现在有这样的一个文件food.tab
内容如下:
Room_NumberBreakfast LunchDinner
290Bagel PizzaSalmon
301 OrangePizzaChicken ala King
349 Sweet RollSaladTofu and Vegetables
500 OmeletSausageVeal
702 EggsTuna fishCheese 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 SystemImports System.
IOImports System.
CollectionsImports System.
DataImports System.Text
Module modXML
Sub Main()
Dim strXML
As StringstrXML
= delimitedDataSet(vbTab, "c:/food.tab")
'你可能需要进行必要的修改
End SubFunction delimitedDataSet(
ByVal strDelimiter
As String, _
ByVal strFilePath
As String)
As StringDim oDS
As New DataSet()
Dim strFields
As StringDim 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 ColumnsFor Each strFields In oSR.ReadLine().
Split(strDelimiter)
oDS.Tables(0).Columns.Add(strFields)
Next'现在添加rowsoTable
= oDS.Tables(0)
While (oSR.Peek()
> -1)
oRows
= oTable.NewRow()
For Each strFields In oSR.ReadLine().
Split(strDelimiter)
oRows(intCounter)
= strFields
intCounter
= intCounter
+ 1
NextintCounter
= 0
oTable.Rows.Add(oRows)
End WhileReturn oDS.GetXml()
'oDS.WriteXml("c:/food.xml")
'或者将它写到硬盘上
End Function……