Here’s the relevant (C#.NET) code:
WebRequest webRequest = System.Net.WebRequest.Create(authenticationUrl);
UTF8Encoding encoding = new UTF8Encoding();
...
var webResponse = webRequest.GetResponse();
var webResponseLength = webResponse.ContentLength;
byte[] responseBytes = new byte[webResponseLength];
webResponse.GetResponseStream().Read(responseBytes, 0, (int)webResponseLength);
var responseText = encoding.GetString(responseBytes);
webResponse.Close();
Here’s what the value of responseText looks like (as copied from Visual Studio while debugging the above code):
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<responseblock version=\"3.67\">\n <requestreference>X3909254</requestreference>\n <response type=\"ERROR\">\n <timestamp>2012-04-16 13:53:59</timestamp>\n <error>\n <message>Invalid field</message>\n <code>30000</code>\n <data>baseamount</data>\n </error>\n </response>\n</responseblock>\n"
Why are there seemingly escape characters (e.g. \") in the response? Is this due to the way in which I’m converting the response stream to a string? What should I do instead (so that the value stored in the variable responseText can be parsed as ‘standard’ XML)?
UPDATE – some more code that I was using:
var resultXML = XElement.Parse(responseText);
...
int errorCode = (int)(resultXML.Element("error").Element("code"));
The problem was that the element error isn’t a direct child of the root element of resultXML, hence my apparent inability to reference error (or its child element code).
You see these characters only while debugging. I guess the purpose is that you can copy the whole string and insert it directly into C# code for further testing. Also, it’s to be able to represent the whole string as one line.
But all the
\n‘s will be converted to real line breaks when you access the string in your code. So you can safely parse it.P.S. Why are you invoking the web request manually? Visual Studio will generate the stub code for you if you use the “Add Web Reference” feature in your solution tree. Then you don’t have to care about XML – you’ll work with objects Visual Studio generates basing on the WSDL description.