如何从XML字符串获取DataSet
以往可能从XML文件获取数据集的比较多一些,但是从XML字符串获取DataSet的不是很多,我的想法是:在客户端,用户提交后生成XML字符串,并将这个字符串存到数据库的某个字段,等到我们使用这些XML数据时,只要提取出来,加以分析,显示在不同的Web控件上即可。主要是XmlTextReader的使用。
为方便,下面不涉及数据库(数据库的相关操作省略)。
一:构建一个XML字符串:![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
//创建xml字符串
2
private string GetStrXML()
3
{
4
string strXML = "";
5
strXML += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
6
strXML += "<Root>";
7
strXML += "<DeptName value=\"第一车间\">";
8
for (int i=0;i<3;i++)
9
{
10
strXML += "<SendData value=\""+Convert.ToString(Convert.ToInt32(GetQuarterFirstMonth())+i)+"\">";
11
strXML += "<MeltWeight>";
12
strXML += Convert.ToString(i*10 +1);
13
strXML +="</MeltWeight>";
14
strXML += "<FcatUnitHao>";
15
strXML += Convert.ToString(i*10 +2);
16
strXML +="</FcatUnitHao>";
17
strXML += "<JieDian>";
18
strXML += Convert.ToString(i*10 +3);
19
strXML +="</JieDian>";
20
strXML += "<JieyueValue>";
21
strXML += Convert.ToString(i*10 +4);
22
strXML +="</JieyueValue>";
23
strXML += "<Grade>";
24
strXML += Convert.ToString(i+1)+"级";
25
strXML +="</Grade>";
26
strXML += "<Tijijanglv>";
27
strXML += Convert.ToString((i*10 +5)/5)+"%";
28
strXML +="</Tijijanglv>";
29
strXML +="</SendData>";
30
}
31
strXML +="<Leader>";
32
strXML +="<Name>";
33
strXML +="张三丰";
34
strXML +="</Name>";
35
strXML +="<idea>";
36
strXML +="同意";
37
strXML +="</idea>";
38
strXML +="<desp>";
39
strXML +="本次审批通过! ";
40
strXML +="但是本季度节能比上季度相比,指标有所下降,望各位注意!";
41
strXML +="</desp>";
42
strXML +="<data>";
43
strXML +="2006-04-20";
44
strXML +="</data>";
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
strXML +="</Leader>";
47
strXML += "</DeptName>";
48
strXML += "</Root>";
49
return strXML;
50
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
//由XML字符串生成DataSet
2
private DataSet GetDataSet()
3
{
4
try
5
{
6
string text = GetstrXML();
7
XmlTextReader reader = new XmlTextReader(new StringReader(text));
8
reader.WhitespaceHandling = WhitespaceHandling.None;//
9
DataSet ds = new DataSet();
10
ds.ReadXml( reader );//加载XML到DS中
11
reader.Close();
12
ds.Dispose();
13
return ds;
14
}
15
catch(Exception err)
16
{
17
throw new Exception("GetDataSet方法异常:"+err.Message);
18
19
}
20
}
三.从返回的DataSet中选取不同的DataTable绑定到相应的Web控件上。
根据XML字符串的不同,DataSet中将包含多个DataTable,比如我上面的XML字符串,将返回的DataTable有:DeptName,SendData,Leader三个表。
四.我的最终效果图:
![o_Test.JPG](https://www.cnblogs.com/images/cnblogs_com/zhangzs8896/11595/o_Test.JPG)
五:需要主要的地方
如果测试时,编译有问题,请增加相应的引用(废话).
using System.Xml;
using System.IO;
为方便,下面不涉及数据库(数据库的相关操作省略)。
一:构建一个XML字符串:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
其中里面有一个GetQuarterFirstMonth()方法,是用来获取当前季度的第一个月份的函数,实际测试中,可以随时替换,所以这个函数不贴出来了,以免扰乱视线,呵.
二.根据XML字符串返回DataSet数据集
这里XML串直接来自函数GetstrXML(),在实际业务中,我们大都是从数据库中
获取这个XML字符串的。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
三.从返回的DataSet中选取不同的DataTable绑定到相应的Web控件上。
根据XML字符串的不同,DataSet中将包含多个DataTable,比如我上面的XML字符串,将返回的DataTable有:DeptName,SendData,Leader三个表。
四.我的最终效果图:
五:需要主要的地方
如果测试时,编译有问题,请增加相应的引用(废话).
using System.Xml;
using System.IO;