[筆記] Object 物件(集合) XML 互轉 公用程式

最近小喵有個需求,需要透過WebService,多筆的資料轉入,在以往,都是透過DataTable這樣的弱型別來傳遞,這次想說來試試看怎樣可以傳遞物件集合,撞了一頭包後,最後決定,將物件集合轉為XML,用字串來透過WebService傳遞。這邊就筆記一下 【Object 轉 XML】 與 【XML 轉 Object】,未來有需要可以從這裡找。

緣起

最近小喵有個需求,需要透過WebService,多筆的資料轉入,在以往,都是透過DataTable這樣的弱型別來傳遞,這次想說來試試看怎樣可以傳遞物件集合,撞了一頭包後,最後決定,將物件集合轉為XML,用字串來透過WebService傳遞。這邊就筆記一下 【Object 轉 XML】 與 【XML 轉 Object】,未來有需要可以從這裡找。

 

公用程式

建立一個UtilObj2Xml的類別,將相關程式寫在裡面


Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Text
Imports System.Xml.Serialization

Public Class UtilObj2Xml

    Public Function Obj2Xml(ByVal objType As Type, ByVal obj As Object) As String
        Try
            '將物件進行序列化
            Dim oText As String = String.Empty
            Dim mySerializer As XmlSerializer = New XmlSerializer(objType)
            Dim writer As New IO.StringWriter

            mySerializer.Serialize(writer, obj)
            oText = writer.ToString
            writer.Close()
            Return oText

        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function Xml2Obj(ByVal objType As Type, ByVal oText As String) As Object
        Try
            '將Xml轉為物件並傳回
            Dim mySerializer As XmlSerializer = New XmlSerializer(objType)
            Dim reader As New IO.StringReader(oText)
            Return mySerializer.Deserialize(reader)

        Catch ex As Exception
            Throw
        End Try
    End Function

End Class

 

測試範例:

建一個簡單的類別EmployeeInfo來測試看看


Public Class EmployeeInfo
    Public Property EmpId As String = ""
    Public Property EmpName As String = ""
End Class

 

 

放個按鈕產生範例物件集合,產生後將物件綁到GridView查看一下

再放個按鈕將物件集合,透過公用程式轉為XML字串,放到TextBox中查看結果

接著放一個按鈕,將文字內容中的XML轉回物件集合,將物件集合綁到另一個GridView裡面查看結果

 

以下是相關程式:

測試畫面:


<asp:Button ID="Button1" runat="server" Text="產生物件集合" />
<br />
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
<asp:Button ID="Button2" runat="server" Text="轉為xml" />
<hr />
<asp:TextBox ID="txtRlt" runat="server" TextMode="MultiLine" Rows="10" Columns="40" ValidateRequestMode="Disabled"></asp:TextBox>
<br />
<hr />
<asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
<hr />
<asp:Button ID="Button4" runat="server" Text="Xml2Obj" />
<br />
<asp:GridView ID="GridView3" runat="server">
</asp:GridView>

相關測試程式:


Partial Class Default2
    Inherits System.Web.UI.Page

    Private oEmps As List(Of EmployeeInfo)

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim tEmp As EmployeeInfo
        For y = 1 To 10
            tEmp = New EmployeeInfo
            tEmp.EmpId = "Emp" & Format(y, "00")
            tEmp.EmpName = "Name" & Format(y, "00")
            oEmps.Add(tEmp)
        Next
        ViewState("oEmps") = oEmps
        Me.GridView1.DataSource = oEmps
        Me.GridView1.DataBind()
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        oEmps = ViewState("oEmps")
        If oEmps Is Nothing Then
            oEmps = New List(Of EmployeeInfo)
            ViewState("oEmps") = oEmps
        End If
    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            Dim oUtil As New UtilObj2Xml
            Dim sXML As String = oUtil.Obj2Xml(GetType(List(Of EmployeeInfo)), oEmps)

            Me.txtRlt.Text = sXML
        Catch ex As Exception
            Me.lblMsg.Text = ex.Message
        End Try
    End Sub

    Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim oEmps2 As List(Of EmployeeInfo)
        Dim sXML As String = Me.txtRlt.Text
        Dim oUti As New UtilObj2Xml
        oEmps2 = CType(oUti.Xml2Obj(GetType(List(Of EmployeeInfo)), sXML), List(Of EmployeeInfo))
        Me.GridView3.DataSource = oEmps2
        Me.GridView3.DataBind()
    End Sub
End Class

 

 


以下是簽名:


Microsoft MVP
Visual Studio and Development Technologies
(2005~2019/6)