10

我正在使用XmlReader检索到的 using SqlCommand.ExecuteXmlReader

这是我的输入

当我运行这行代码时:

XDocument currentXDoc = XDocument.Load(ktXmlReader.ReadSubtree());

它第一次工作,按预期读取第一个 Product 节点。

第二次运行时,我得到以下异常:

System.Xml.XmlException:
Message: Unexpected end of file while parsing Name has occurred. Line 1, position 2048.

Stacktrace:    at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseQName(Boolean isQName, Int32 startOffset, Int32& colonPos)
   at System.Xml.XmlTextReaderImpl.ThrowTagMismatch(NodeData startTag)
   at System.Xml.XmlTextReaderImpl.ParseEndElement()
   at System.Xml.XmlTextReaderImpl.ParseElementContent()
   at System.Xml.XmlSubtreeReader.Read()
   at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
   at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
   at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)

我确实发现了这个问题这个问题与我的相似,但我相当确定我的 XML 格式正确(我可以直接从运行存储过程中得到它)

到目前为止我的想法:

  1. 2048 在计算机中是一个非常可疑的数字。我是否在某个地方遇到了 2KB 的XDocument限制XmlReader
  2. 我在 while 循环中的 ktXmlReader.Read() 以某种方式跳过所有其他节点并直接进入 EOF
4

2 回答 2

6

I found the answer here.

My problem was that I was closing the SqlConnection with a using statement when I was getting the XmlReader.

I added the connection to my "using tower of power" and passed it as a parameter, keeping it open, and everything worked perfectly.

于 2013-03-19T19:31:11.410 回答
0

XmlReader 执行是一种方式操作。您可以尝试重置它的位置,或者更简单的方法 -从现有文档创建新阅读器

于 2013-03-19T02:06:55.267 回答