I've done a lot of searching for how to fix this error, and I can't find what's different between everything else and my code. Basically what I'm doing is connecting to a bizTalk .asp service, and its being sent fine, and is recieved fine. It uses streams to get back and forth, and if I were to write it out to a string, it works fine. If I pass the same stream into the constructor of another class that parses the stream to xml, it gives the error. Heres the code:
Encoding encoding = new UTF8Encoding();byte[] data = encoding.GetBytes(request);
// Prepare web request...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(this.path); // connect to mustang
WebResponse response; // POSTed response
myRequest.Method = "POST";
myRequest.ContentType = "text/xml";
//myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
response = myRequest.GetResponse();
Stream resStream = response.GetResponseStream();
//encoding = Encoding.UTF8;
StreamReader read = new StreamReader(resStream);
string line = read.ReadToEnd();
TextReader reader = new StringReader(line);
resStream.Seek(0, SeekOrigin.Begin);
IMWebResponse webResponse = new IMWebResponse(resStream);
resStream.Close();
newStream.Close(); return webResponse;
Heres the important part in IMWebResponse
private
void parseHeader(Stream xmlResponse, TextReader reader){ XmlDocument xDoc = new XmlDocument();
xDoc.Load(reader); //heres where the error is thrown. XmlNodeList xmlList = xDoc.SelectNodes("PNAResponse/TransactionHeader");
header.ErrorNumber = xmlList.Item(0).SelectSingleNode("ErrorStatus")["ErrorNumber"].InnerText;
header.ErrorStatus = xmlList.Item(0).SelectSingleNode("ErrorStatus").InnerText;
header.TransID = xmlList.Item(0).SelectSingleNode("TransactionID").InnerText;
header.TimeStamp = DateTime.Parse(xmlList.Item(0).SelectSingleNode("TimeStamp").InnerText);
}
I've been doing a lot of playing around with this code, but the original code was something copied and pasted. The original error was the root element is missing, but now with this current code the page wont actually load, it just kinda sits there trying to load. www.doublenatural.com/test.aspx is where you can see the lack of action. Its an interesting error, and I'm having the darndest time figuring it out. Any thoughts

the age old problem of "the root element is missing"
Alexander Zeitler
No error when doing that, but the page won't load. It loads and loads and loads and then times out and throws a Thread was being aborted error. Heres the code:
Encoding encoding = new UTF8Encoding(); byte[] data = encoding.GetBytes(request); // Prepare web request... HttpWebRequest myRequest= (
HttpWebRequest)WebRequest.Create(this.path); // connect to mustang WebResponse response; // POSTed responsemyRequest.Method =
"POST";myRequest.ContentType =
"text/xml";myRequest.Accept =
"text/xml"; Stream newStream = myRequest.GetRequestStream(); // Send the data.newStream.Write(data, 0, data.Length);
response = myRequest.GetResponse();
Stream resStream = response.GetResponseStream(); IMWebResponse webResponse = new IMWebResponse(resStream); return webResponse;Now I'm really confused.
David Sadler
These "garbage" characters are either Unicode's Byte Order Mark or UTF-8 prologue.
When XmlReader opens stream it does quite untrivial work of detecting what is the stream format (UTF-8, ANSII, Unicode, ...) and what code page this stream uses. For this it reads BOM and XML Declaration, switching stream encoder "on the fly". Don't try this at home.
The best you can do for your program is passing input stream directly to XmlReader (or to XmlDocument). It will handle it appropriately.
To test how reader part works you can read file :
XmlReader r = XmlReader.Create(stream);
while(r.Read()) {
// print reader properties
}
jwagner20
Wasif235288
Does the response stream contain well-formed XML Is there an XML declaration (e.g. < xml version="1.0" encoding="UTF-8" >) at the beginning XML can be encoded using various encodings so unless the encoding is UTF-8 or UTF-16 and/or a byte order mark is present the XML document needs an XML declaration declaring the encoding. And XmlReader/XmlTextReader when passes a stream will look at the XML declaration to decide which encoding to use to decode the stream.
As for the error, can you write that stream to a file and post a URL so that we could check the contents with an XML parser It is hard to diagnose what is wrong with the stream or with XmlTextReader without seeing the contents.
For completeness, it can also help if you show us the HTTP response headers, in particular Content-Type including charset parameter.
David Parreira
I can't tell where the problem is. IE loads http://www.doublenatural.com/test.aspx just fine. And .NET can handle it too as this sample works fine here for me:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"http://www.doublenatural.com/test.aspx");
xmlDocument.Save(Console.Out);
As for the encoding problem, Oleg has already pointed out that you should simply consume the stream you get and pass that to the XML API you want to use (e.g. XmlDocument and its Load method, or XmlReader), that way your code does not have to specify an Encoding in your .NET code, rather the XML parser will look at the XML declaration in the stream.
djroelfsema
Why do you read response stream into a string and then pass a reader over this string instead of using response stream directly
Ying06
here is the reponse string
< xml version="1.0" encoding="ISO-8859-1" >
<PNAResponse><Version>2.0</Version><TransactionHeader><SenderID>987654321</SenderID><ReceiverID>123456789</ReceiverID><ErrorStatus ErrorNumber=""></ErrorStatus><DocumentID>{391b832e-8bc8-4ac4-8d08-9433aee44d83}</DocumentID><TransactionID> </TransactionID><TimeStamp>2007-02-13T08:03:16</TimeStamp></TransactionHeader><PriceAndAvailability SKU="34807F" Quantity="1"><Price>817.39</Price><SpecialPriceFlag></SpecialPriceFlag><ManufacturerPartNumber>312-03545</ManufacturerPartNumber><ManufacturerPartNumberOccurs></ManufacturerPartNumberOccurs><VendorNumber>3987</VendorNumber><Description>EXCHANGE SVR 2007 EN OLP</Description><ReserveInventoryFlag>N</ReserveInventoryFlag><AvailableRebQty>0</AvailableRebQty><Branch ID="10" Name="Vancouver"><Availability>0</Availability><OnOrder>0</OnOrder><ETADate></ETADate></Branch><Branch ID="40" Name="Toronto"><Availability>0</Availability><OnOrder>0</OnOrder><ETADate></ETADate></Branch></PriceAndAvailability></PNAResponse>
It is well formed, I have no control of that. I think the problem may be that I am using UTF8 encoding and this specifies ISO-8859-1. Could that be the case If so, how might I be able to fix it http://www.doublenatural.com/test.aspx writes the output of the above response string. its just a Response.write() as its a shared server and security is tight. The StreamReader.CurrentEncoding returns UTF-8.
Also...I just remembered that the response XML is from a Microsoft BizTalk server. That may help with the problem. On another note, I tried using XmlDocument.LoadXml(string xml). It doesn't throw an exception at load, but it won't parse any of the items.
WinFormsUser13232
Thanks for the input. In the end it may help, but I'm not currently dealing with xml files, I'm just dealing with streams. I'm not sure which is worse
.
I am willing to let people take a look at the complete source code and see where I might be going wrong if they want. Just keep the comments about my programming habits to your self
. You can take a look at it here www.doublenatural.com/source.zip
Wally9633
I was able to fix the error by opening my XML file in a plain text editor and I saw some garbled characters before the XML declaration which I deleted. After this everything worked fine. For some reason these garbled characters didn't show up in Visual Studio but did in the plain text editor. The editor was HTML-Kit, by the way. I don't know which application introduced those characters but they turned up occasionally and I just needed to delete them in the other editor.
Again, sorry if this is irrelevant
Regards,
Mark
David Beavonn
For some reason, if I were to print out the stream to a string, it will print to the screen, however whenever I pass the stream to the XMLTextReader it won't work and the error is thrown. So, what is the correct encoding scheme that I should be using if I want to load an XmlDocument with a XmlTextReader that has gotten its stuff from a stream I'm thinking its UTF8 Could this be the problem
heres how the flow goes:
build request string -> encode request (UTF8 ) -> send HttpWebRequest -> receive WebResponse -> get response stream ->put stream into XmlTextReader -> load xmlTextReader into xmlDocument (Exception thrown here) -> parse document
Unless the stream is getting corrupted in some way or another by being passed through one of my class constructors (isn't .net supposed to prevent such things ) it should work. Is there another possible way of doing this
Whoisit
Hi,Did you solve this problem
I am also getting the same problem while I access some remote web services. Could this be a problem with the web service itself
I have observed that this is not a consistent behaviour as well! The web service works fine as well sometimes and I get the desired results. But when I continously try to access the same,it fails between consequent requests.
Regards