XML與DataSet的相互轉換

2021-09-22 09:10:08 字數 2371 閱讀 1630

xmldatasetconvert 該類提供了四種方法: 

1、將xml物件內容字串轉換為dataset 

2、將xml檔案轉換為dataset 

3、將dataset轉換為xml物件字串 

4、將dataset轉換為xml檔案

xmldatasetconvert.cs 

using system;

using system.collections.generic;

using system.text;

using system.data;

using system.io;

using system.xml;

namespace xmldesign

catch (system.exception ex)

finally

}//將xml檔案轉換為dataset

public

static dataset convertxmlfiletodataset(string xmlfile)

catch (system.exception ex)

finally

}//將dataset轉換為xml物件字串

public

static

string convertdatasettoxml(dataset xmlds)

catch (system.exception ex)

finally

}//將dataset轉換為xml檔案

public

static

void convertdatasettoxmlfile(dataset xmlds, string xmlfile)

catch (system.exception ex)

finally}}

} 使用例子

using system;

using system.collections.generic;

using system.text;

using system.xml;

using system.data;

namespace xmldesign

\",包含個表", ds.datasetname, ds.tables.count);

foreach (datatable dt in ds.tables)

#endregion

#region 構造乙個dataset,並轉換為xml字串

dataset ds1 = new dataset();

datatable dt1 = new datatable();

dt1.tablename = "test";

dt1.columns.add("id");

dt1.columns.add("name");

dt1.rows.add("i001", "hekui");

dt1.rows.add("i002", "liyang");

datatable dt2 = new datatable();

dt2.tablename = "test1";

dt2.columns.add("bookid");

dt2.columns.add("bookname");

dt2.rows.add("b001", "書本1");

dt2.rows.add("b002", "書本2");

ds1.tables.add(dt1);

ds1.tables.add(dt2);

ds1.datasetname = "方案";

string xmlout = xmldatasetconvert.convertdatasettoxml(ds1);

#endregion

#region 轉換乙個xml字串為乙個dataset

dataset ds2 = new dataset();

ds2 = xmldatasetconvert.convertxmltodataset(xmlout);

console.writeline("資料集名為\"\",包含個表", ds2.datasetname, ds2.tables.count);

foreach (datatable dt in ds2.tables)

#endregion

#region 轉換乙個dataset為乙個xml檔案

xmldatasetconvert.convertdatasettoxmlfile(ds2, "c:\\adadsda1.xml");

#endregion

console.readline();

}private

static

void printtablename(string tablename)}}

XML與DataSet的相互轉換

一 xml 與 dataset 的關係如下圖所示 二 dataset 物件的常用方法如下 a.使用readxml 方法 從檔案或流中載入xml 資料,填充dataset 物件。dataset 物件.readxml 檔案路徑字串 stream 物件,xmlreadmode 列舉值 可以省略 b.使用w...

XML與DataSet的相互轉換

xmldatasetconvert 該類提供了四種方法 1 將xml物件內容字串轉換為dataset 2 將xml檔案轉換為dataset 3 將dataset轉換為xml物件字串 4 將dataset轉換為xml檔案 1 2 3 using system 4 using system.collec...

XML與DataSet的相互轉換類

xmldatasetconvert 該類提供了四種方法 1 將xml物件內容字串轉換為dataset 2 將xml檔案轉換為dataset 3 將dataset轉換為xml物件字串 4 將dataset轉換為xml檔案 xmldatasetconvert.cs using system using ...