2013-03-19 75 views
8

我正在使用SqlCommand.ExecuteXmlReader檢索到的XmlReaderSystem.Xml.XmlException:解析時文件意外結束髮生名稱

Here is my input

當我運行這行代碼:

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

它工作在第一時間,第一產品節點讀取預期。

運行第二次,我得到以下異常:

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) 

我確實發現this questionthis question這是與我相似,但我相當肯定是格式良好的XML我(我可以直接從運行一個存儲過程)

我的想法而已獲得它:

  1. 2048是電腦一個非常可疑的號碼。我在XDocumentXmlReader的某處遇到2KB的限制嗎?
  2. 我ktXmlReader.Read()在while循環以某種方式跳過所有其他節點,而直接進入到EOF
+0

http://forums.asp.net/t/1191911.aspx – Codeman 2013-03-19 19:18:02

回答

4

我找到了答案here

我的問題是,當我得到XmlReader時,我正在用using語句關閉SqlConnection。

我添加了連接到我的「使用權力塔」,並將它作爲參數傳遞,保持打開狀態,並且一切正常。

0

的XmlReader執行是一個單向操作。你可以嘗試重置它的位置,或者更容易 - create a new reader from existing document

+0

有趣的是,你有一個如何從讀者調用XNode.Create的代碼示例,還是需要重構我現有的代碼?它看起來像這樣做,我必須從XDocument創建一個XmlReader,而不是XmlReader的XDocument – Codeman 2013-03-19 02:10:17

+0

此外,我不試圖在多個方向閱讀。我打電話給ReadSubTree閱讀我的輸入中的單個子樹(產品節點) – Codeman 2013-03-19 02:13:55

+0

@ Pheonixblade9:我只知道如何從閱讀器加載文檔,而不是節點。但有什麼區別?除了你會得到多個文件,而不是節點。 – abatishchev 2013-03-19 02:45:03