关于把xml格式的字符串 写入dataset遇到的问题
xml字符串为:
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/">
<row> <TimeStamp>2011-6-30 14:00:00</TimeStamp> <TagName>newtag1</TagName> <Value>0</Value> <OPCQuality>0</OPCQuality> </row>
<row> <TimeStamp>2011-6-30 14:41:53</TimeStamp> <TagName>newtag1</TagName> <Value>888</Value> <OPCQuality>192</OPCQuality> </row>
<row> <TimeStamp>2011-6-30 14:42:28</TimeStamp> <TagName>newtag1</TagName> <Value>5567</Value> <OPCQuality>192</OPCQuality> </row>
<row> <TimeStamp>2011-6-30 14:43:04</TimeStamp> <TagName>newtag1</TagName> <Value>0</Value> <OPCQuality>0</OPCQuality> </row>
<row> <TimeStamp>2011-7-1 9:26:36</TimeStamp> <TagName>newtag1</TagName> <Value>1233421</Value> <OPCQuality>192</OPCQuality> </row>
<row> <TimeStamp>2011-7-1 9:27:34</TimeStamp> <TagName>newtag1</TagName> <Value>0</Value> <OPCQuality>0</OPCQuality> </row>
</string>
读取代码如下:
private void btnQry_Click(object sender, EventArgs e)
{
string strdata;
strdata = string.Empty;
StringReader stream = null;
XmlTextReader reader = null;
try
{
localhost.Service web = new localhost.Service();
strdata = web.TagQuery(TBoxTgName.Text, dtpStart.Text, dtpEnd.Text);
DataSet ds = new DataSet();
stream = new StringReader("@" + strdata);
reader = new XmlTextReader(stream);
ds.ReadXml(reader); //错误在这里发生,提示:“根级别上的数据无效。 行 1,位置 1....”
dgv.DataSource = ds;
dgv.Refresh();
TBoxStatus.Text = "数据采集成功!";
}
catch (Exception ex)
{
TBoxStatus.Text = "数据采集失败!"+ ex.Message;
}
}
[解决办法]
路过 ,围观