简体   繁体   English

System.Xml.XmlException:解析名称时发生了意外的文件结尾

[英]System.Xml.XmlException: Unexpected end of file while parsing Name has occurred

I'm using an XmlReader retrieved using SqlCommand.ExecuteXmlReader . 我正在使用通过SqlCommand.ExecuteXmlReader检索的XmlReader

Here is my input 这是我的意见

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: 到目前为止,我的想法是:

  1. 2048 is a very suspicious number in computers. 2048是计算机中非常可疑的数字。 Am I running into a 2KB limit somewhere in XDocument or XmlReader ? 我在XDocumentXmlReader某个地方遇到2KB的限制吗?
  2. My ktXmlReader.Read() in the while loop is somehow skipping all the other nodes and going straight to the EOF 我的while循环中的ktXmlReader.Read()以某种方式跳过了所有其他节点并直接进入EOF

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.

相关问题 Windows Identity Foundation System.Xml.XmlException:意外的文件结尾 - Windows Identity Foundation System.Xml.XmlException: Unexpected end of file 解析XML导致System.Xml.XmlException:找不到元素'Customer' - Parsing XML leads to System.Xml.XmlException: Element 'Customer' was not found System.XML.XmlException:“ ”是意外标记。 预期的令牌是 ';' - System.XML.XmlException: ' ' is an unexpected token. The expected token is ';' C#Xml System.Xml.dll中发生了类型为'System.Xml.XmlException'的未处理的异常 - C# Xml An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll System.Xml.dll中发生“ System.Xml.XmlException”,但未在用户代码中处理 - 'System.Xml.XmlException' occurred in System.Xml.dll but was not handled in user code Visual Basic'System.Xml.XmlException'发生在System.Xml.dll错误中 - Visual basic 'System.Xml.XmlException' occurred in System.Xml.dll error 如何捕获system.xml.xmlexception - How to catch system.xml.xmlexception System.Xml.XmlException-根元素丢失 - System.Xml.XmlException - Root element is missing EntityFramework.dll中出现“System.Xml.XmlException”类型的异常,但未在用户代码中处理 - An exception of type 'System.Xml.XmlException' occurred in EntityFramework.dll but was not handled in user code W8电话System.Xml.XmlException& - W8 Phone System.Xml.XmlException &
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM