將具有特殊格式的檔案轉化為xml檔案

來源:互聯網
上載者:User
xml 假如我們現在有這樣的一個檔案food.tab
內容如下:
Room_Number  Breakfast   Lunch      Dinner
290                    Bagel       Pizza      Salmon
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


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。