簡體   English   中英

System.Xml.XmlException:解析名稱時發生了意外的文件結尾

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

我正在使用通過SqlCommand.ExecuteXmlReader檢索的XmlReader

這是我的意見

當我運行以下代碼行時:

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格式正確(我可以通過運行sproc來直接獲取它)

到目前為止,我的想法是:

  1. 2048是計算機中非常可疑的數字。 我在XDocumentXmlReader某個地方遇到2KB的限制嗎?
  2. 我的while循環中的ktXmlReader.Read()以某種方式跳過了所有其他節點並直接進入EOF

我在這里找到了答案。

我的問題是,在獲取XmlReader時,我正在使用using語句關閉SqlConnection。

我將連接添加到“使用電力塔”中,並將其作為參數傳遞,保持打開狀態,一切正常。

XmlReader執行是一種操作方式。 您可以嘗試重置其位置,或者更容易- 從現有文檔中創建一個新的閱讀器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM