日期:2014-05-18  浏览次数:20485 次

用xmlReader.Read()读数据库,但是只返回单数行~
STAFF表中有5行记录,但是编译后只显示单数行1,3,5的记录

private   string   ExecuteXmlReaderSqlCommand()
{
string   cmdtext= "select   Staff_Name   from   Staff   for   Xml   auto   ";
                  SqlConnection   MyConnection=new   SqlConnection(sqlconnectionstring);
                  SqlCommand   MyCommand=new   SqlCommand(cmdtext,MyConnection);
MyConnection.Open();
XmlReader   xmlReader=MyCommand.ExecuteXmlReader();
string   xmlString= " ";
while(xmlReader.Read())
                {  
xmlString+=xmlReader.ReadOuterXml()+ "\n ";
}
xmlReader.Close();
MyConnection.Close();
return(xmlString);
}

------解决方案--------------------
估计是xmlReader.ReadOuterXml方法不仅仅是读取了OuterXml数据,还向前移动了1条记录(类似Read()方法)。然后又调用Read()方法,所以会跳过双数行。

这只是猜测,没试过,楼主自己试试,xmlReader用的比较少,不太熟。
------解决方案--------------------
正如 BearRui(开心熊 | 第1个JAVA项目终于完成) 的猜测一样,

当你调用 ReadOuterXml 的时候,节点类型是 Element,读取器会推进的,
而节点类型是 Attribute 则不会

你应该考虑使用,具体的读取节点值的方法,

XmlReader.Value
XmlReader.ReadContentAs


MSDN 对 ReadOuterXm 的说明:

此方法以下面的方式处理元素和属性节点:

节点类型
调用前的位置
XML 片段
返回值
调用后的位置

Element
在 item1 开始标记上。
<item1> text1 </item1> <item2> text2 </item2>
<item1> text1 </item1>
在 item2 开始标记上。

Attribute
在 attr1 属性节点上。
<item attr1= "val1 " attr2= "val2 "> text </item>
attr1= "val1 "
保留在 attr1 属性节点上。


详细信息:
http://msdn2.microsoft.com/zh-cn/library/system.xml.xmlreader.readouterxml(VS.80).aspx

------解决方案--------------------
或者在读完后有语句能后退一条记录吗?
==========
不性, XmlReader 提供只进模型
------解决方案--------------------
读了两次了