2014年5月25日 星期日

vbnet save xml / read xml

參考:How to save a DataSet as an XML file
參考:How Can write a DataSet as XML through VB.Net & C#?
參考:Export Datatable to xml
參考 MSDN:DataTable.WriteXml 方法

-------------
vb net datatable to xml

#1
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Column1"))
dt.Columns.Add(New DataColumn("Column2"))
Dim r = dt.NewRow
r.Item(0) = "Value1"
r.Item(1) = "Value2"
dt.Rows.Add(r)
r = dt.NewRow
r.Item(0) = "Value3"
r.Item(1) = "Value4"
dt.Rows.Add(r)
Dim ds As New DataSet
ds.Tables.Add(dt)

Dim simpleresult As String = ds.GetXml


Dim result As String
Using ms As New IO.MemoryStream()
    ds.WriteXml(ms, System.Data.XmlWriteMode.WriteSchema)
    result = System.Text.Encoding.UTF8.GetString(ms.ToArray)
End Using


#2
Private Sub btnWriteXMLData_Click(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles btnWriteXMLData.Click
        Dim dsSales As New DataSet()
        Dim cn As New SqlConnection _
                    ("data source=localhost;initial catalog=pubs;user id=sa")
        Dim daAuthors As New SqlDataAdapter("select * from sales ", cn)
        Dim daPublishers As New SqlDataAdapter("select * from stores ", cn)

        ' Load data from database
        daAuthors.Fill(dsSales, "Sales")
        daPublishers.Fill(dsSales, "Stores")

        ' Write XML to file
        dsSales.WriteXml("XMLFile.xml")

End Sub


#3

Dim SB As New StringBuilder
Dim SW As New IO.StringWriter(SB)
Items.WriteXml(SW, Data.XmlWriteMode.WriteSchema)



#4
Dim ds As DataSet

'  ... Fill the DataSet with data from your database ...

ds.WriteXml("XML/MyData.xml", XmlWriteMode.WriteSchema)

Dim ds As DataSet = new DataSet()
ds.ReadXml("XML/MyData.xml", XmlReadMode.ReadSchema)


#5
DS.Tables(0).DefaultView.Sort = "ID ASC"
Dim outputDataTable = DS.Tables(0).DefaultView.ToTable()
outputDataTable.WriteXml("c:\blah\user.xml")

沒有留言:

張貼留言