System.Xml.XmlException: Unexpected end of file while parsing name occurred

I am using XmlReader extracted using SqlCommand.ExecuteXmlReader .

Here is my input

When I run this line of code:

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

it works for the first time, reading the first node product, as expected.

The second time it starts, 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 found this question and this question , which was similar to mine, but honestly, of course, my XML is well formed (I can get it directly from sproc)

My ideas so far:

  • 2048 is a very suspicious number on computers. Am I encountering a XDocument limit somewhere in XDocument or XmlReader ?
  • My ktXmlReader.Read () in a while loop somehow skips all the other nodes and goes straight to EOF
+8
c # xml linq linq-to-xml xmlreader
source share
2 answers

I found the answer here .

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

I added a connection to my β€œuse of the power tower” and passed it as a parameter, leaving it open, and everything worked perfectly.

+4
source share

Running XmlReader is a one-way operation. You can try resetting its position or, what is easier, create a new reader from an existing document .

0
source share

All Articles