[英]System.Xml.XmlException: Unexpected end of file while parsing Name has occurred
I'm using an XmlReader
retrieved using SqlCommand.ExecuteXmlReader
. 我正在使用通过
SqlCommand.ExecuteXmlReader
检索的XmlReader
。
When I run this line of code: 当我运行以下代码行时:
XDocument currentXDoc = XDocument.Load(ktXmlReader.ReadSubtree());
it works the first time, reading in the first Product node as expected. 它第一次运行,按预期方式读取第一个Product节点。
The second time it runs, I get the following exception: 第二次运行时,出现以下异常:
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)
I did find this question and this question that were similar to mine, but I'm fairly sure my XML is well-formed (I can get it directly from running a sproc) 我确实找到了这个问题 , 这个问题与我的类似,但是我相当确定我的XML格式正确(我可以通过运行sproc来直接获取它)
My ideas so far: 到目前为止,我的想法是:
XDocument
or XmlReader
? XDocument
或XmlReader
某个地方遇到2KB的限制吗? I found the answer here . 我在这里找到了答案。
My problem was that I was closing the SqlConnection with a using statement when I was getting the XmlReader. 我的问题是,在获取XmlReader时,我正在使用using语句关闭SqlConnection。
I added the connection to my "using tower of power" and passed it as a parameter, keeping it open, and everything worked perfectly. 我将连接添加到“使用电力塔”中,并将其作为参数传递,保持打开状态,一切正常。
XmlReader execution is one way operation. XmlReader执行是一种操作方式。 You may try to reset it's position, or what is easier - create a new reader from existing document .
您可以尝试重置其位置,或者更容易- 从现有文档中创建一个新的阅读器 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.