XML與DataSet的相互轉換

2022-07-10 04:21:11 字數 3815 閱讀 8693

xmldatasetconvert 該類提供了四種方法:

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

2、將xml檔案轉換為dataset

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

4、將dataset轉換為xml檔案

1 

2 3 using system;

4 using system.collections.generic;

5 using system.text;

6 using system.data;

7 using system.io;

8 using system.xml;

9 10 namespace xmldesign

11 28 catch (system.exception ex)

29

32 finally

33

36 }

37 38 //將xml檔案轉換為dataset

39 public static dataset convertxmlfiletodataset(string xmlfile)

40

56 catch (system.exception ex)

57

60 finally

61

64 }

65 66 //將dataset轉換為xml物件字串

67 public static string convertdatasettoxml(dataset xmlds)

68

88 catch (system.exception ex)

89

92 finally

93

96 }

97 98 //將dataset轉換為xml檔案

99 public static void convertdatasettoxmlfile(dataset xmlds,string xmlfile)

100

124 catch( system.exception ex )

125

128 finally

129

132 }

133

134 }

135 }

136 //使用示例

137

138 using system;

139 using system.collections.generic;

140 using system.text;

141 using system.xml;

142 using system.data;

143

144 namespace xmldesign

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

158 foreach(datatable dt in ds.tables)

159 ;

162 #endregion

163

164 //構造乙個dataset,並轉換為xml字串

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

166 dataset ds1 = new dataset();

167 datatable dt1 = new datatable();

168 dt1.tablename = "test";

169 dt1.columns.add("id");

170 dt1.columns.add("name");

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

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

173

174 datatable dt2 = new datatable();

175 dt2.tablename = "test1";

176 dt2.columns.add("bookid");

177 dt2.columns.add("bookname");

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

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

180

181 ds1.tables.add(dt1);

182 ds1.tables.add(dt2);

183 ds1.datasetname = "方案";

184 string xmlout = xmldatasetconvert.convertdatasettoxml(ds1);

185 #endregion

186

187 //轉換乙個xml字串為乙個dataset

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

189 dataset ds2 = new dataset();

190 ds2 = xmldatasetconvert.convertxmltodataset(xmlout);

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

192 foreach (datatable dt in ds2.tables)

193 ;

196 #endregion

197

198 //轉換乙個dataset為乙個xml檔案

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

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

201 #endregion

202

203 console.readline();

204 }

205

206 private static void printtablename(string tablename)

207

210 }

211 }

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檔案 xmldatasetconvert.cs using system using ...

XML與DataSet的相互轉換類

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