■■■

2016年4月15日金曜日

DataTableのデータと状態をXMLに保存しておく方法



DataTableのデータと状態をXMLに保存しておく方法

VB.NET


        ' 仮のデータテーブル
        Dim dtMain As New DataTable()
        ' XMLWriter作成
        Dim xmlWriter As System.Xml.XmlTextWriter = Nothing
        Try
            ' XMLWriter準備
            Dim fileStream As New System.IO.FileStream("c:\dataset.xml", System.IO.FileMode.Create)
            xmlWriter = New System.Xml.XmlTextWriter(fileStream, System.Text.Encoding.UTF8)
            xmlWriter.Formatting = System.Xml.Formatting.Indented
            ' 書込み
            dtMain.WriteXml(xmlWriter)
        Finally
            xmlWriter.Close()

        End Try



C#


            // 仮のデータテーブル
            DataTable dtMain = new DataTable();
            // XMLWriter作成
            System.Xml.XmlTextWriter xmlWriter = null;
            try
            {
                // XMLWriter準備
                System.IO.FileStream fileStream = new System.IO.FileStream(@"c:\dataset.xml", System.IO.FileMode.Create);
                xmlWriter = new System.Xml.XmlTextWriter(fileStream, System.Text.Encoding.UTF8);
                xmlWriter.Formatting = System.Xml.Formatting.Indented;
                // 書込み
                dtMain.WriteXml(xmlWriter);
            }
            finally
            {
                xmlWriter.Close();

            }


■■■