anup_daware Posted February 6, 2007 Posted February 6, 2007 (edited) Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read half of the xml and suddenly an exception: “Unexpected end of file while parsing Name has occurred” is being thrown. Following is the part o xml I am trying to read: <CHECK_ITEM_OUT> <ITEM id=""> <!-- id = [material] --> <ITM_NUMBER>1</ITM_NUMBER> <MATERIAL>001</MATERIAL> <DEALER_CODE>DEL_One</DEALER_CODE> <BRAND>Bridgestone</BRAND> <HIERARCHY>HAR001</HIERARCHY> <NET_PRICE>123.12</NET_PRICE> <CURRENCY>EURO</CURRENCY> </ITEM> <ITEM id=""> <!-- id = [material] --> <ITM_NUMBER>2</ITM_NUMBER> <MATERIAL>002</MATERIAL> <DEALER_CODE>DEL_Two</DEALER_CODE> <BRAND>Firestone</BRAND> <HIERARCHY>HAR002</HIERARCHY> <NET_PRICE>453.12</NET_PRICE> <CURRENCY>EURO</CURRENCY> </ITEM> <ITEM id=""> <!-- id = [material] --> <ITM_NUMBER>3</ITM_NUMBER> <MATERIAL>003</MATERIAL> <DEALER_CODE /> <BRAND>FIRESTONE</BRAND> <HIERARCHY>HAR001</HIERARCHY> <NET_PRICE>24.12</NET_PRICE> <CURRENCY>EURO</CURRENCY> </ITEM> </CHECK_ITEM_OUT> When I am trying to read the <BRAND>FIRESTONE</BRAND> (In Third item in the list), I am getting the “Unexpected end of file while parsing Name has occurred” exception. I am able to read the values from <BRAND> element for first two elements. Following is the code I am using for reading the xml. case "BRAND": if (!xmlTextReader.IsEmptyElement) productInfo.Brand = xmlTextReader.ReadElementContentAsString(); break; I also have searched net for this problem but I found nothing useful Please let me know if I am doing something wrong or missing on something. Thanks, Anup Daware Edited February 6, 2007 by PlausiblyDamp Quote
*Experts* Nerseus Posted February 6, 2007 *Experts* Posted February 6, 2007 Is it possible that between the Brand and Hierarchy elements there's a special character, maybe character(0) in the string? I'm wondering if there's something that may be triggering an end of file unexpectedly even though the string "looks" good. Can you save the string to a file and try to open it with XmlDocument? I usually prefer XmlDocument to the streaming object since it validates the "easy" stuff (like missing elements or closing elements, etc.) while it loads the document. As long as it's relatively small (even a few megabytes) the XmlDocument is lightning fast. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
anup_daware Posted February 7, 2007 Author Posted February 7, 2007 Hi Nerseus, I have to follow the SAX approach as the main requirement of the project is Speed of execution, so cant take any chances. But I ll surely try XmlDocument for testing. Thanks for the suggestion, Anup Daware Quote
anup_daware Posted February 7, 2007 Author Posted February 7, 2007 Solved :) Hi Group, The problem is solved :) Following lines were creating the problem: StreamWriter streamWriter = new StreamWriter(responseStream); streamWriter.Write(responseXml); //responseXml is a string The the default buffer of StreamWriter is 4kb and the string responseXml is more than that, thus only a portion of my xml was being used by XmlTextWriter which is using responseStream: And that was the reason of Unexpected end of file while parsing Name has occurred exception. Solution: Rather than using the the StreamWriter, I directly used the responseStream.Write; for this I converted the string to byte array. Following is the code for it. String responseXml = GetResponseStream(uri, searchRequestXML); System.Text.UTF8Encoding ob = new UTF8Encoding(); byte[] arr2 = ob.GetBytes(responseXml); responseStream.Write(arr2,0,arr2.Length); responseStream.Seek(0, SeekOrigin.Begin); XmlTextReader xmlTextReader = new XmlTextReader(responseStream) Well the conclusion is StreamWriter has a default size of 4KB which is not increased dynamically, and this is really unexpected. Best Regards, Anup Daware Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.