10

I'm using an XmlReader retrieved using SqlCommand.ExecuteXmlReader.

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.

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)

My ideas so far:

  1. 2048 is a very suspicious number in computers. Am I running into a 2KB limit somewhere in XDocument or XmlReader?
  2. My ktXmlReader.Read() in the while loop is somehow skipping all the other nodes and going straight to the EOF
Community
  • 1
  • 1
Codeman
  • 12,157
  • 10
  • 53
  • 91

2 Answers2

6

I found the answer here.

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

I added the connection to my "using tower of power" and passed it as a parameter, keeping it open, and everything worked perfectly.

Codeman
  • 12,157
  • 10
  • 53
  • 91
0

XmlReader execution is one way operation. You may try to reset it's position, or what is easier - create a new reader from existing document.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Interesting, do you have a code sample of how I could call XNode.Create from a reader, or would that require a refactor of my existing code? It looks like to do that, I have to create an XmlReader from an XDocument, rather than an XDocument from an XmlReader – Codeman Mar 19 '13 at 02:10
  • Also, I am not trying to read in multiple directions. I'm calling ReadSubTree to read individual subtrees (Product nodes) in my input – Codeman Mar 19 '13 at 02:13
  • @Pheonixblade9: I know only how to load a document from reader, not a node. But what is the difference? Except you will get multiple documents instead of nodes. – abatishchev Mar 19 '13 at 02:45
  • @Pheonixblade9: From my XmlReader debugging experience, after reading first node it can be in incorrect state. Check that calling reader.Read() one or more times. Also in debug you can do a trick: `string t = XDocument.Load(parser).ToString()` to see what is current state and what result would you get. – abatishchev Mar 19 '13 at 02:46