関連する(C#.NET)コードは次のとおりです。
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();
の値は次のresponseText
ようになります(上記のコードのデバッグ中にVisual Studioからコピーされたもの)。
"<?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"
\"
応答にエスケープ文字(例)が含まれているように見えるのはなぜですか?これは、応答ストリームを文字列に変換する方法によるものですか?代わりに何をすべきですか(変数に格納されている値responseText
を「標準」XMLとして解析できるようにするため)?
更新–私が使用していたいくつかのコード:
var resultXML = XElement.Parse(responseText);
...
int errorCode = (int)(resultXML.Element("error").Element("code"));
問題は、その要素error
がのルート要素の直接の子ではないため、 (またはその子要素)resultXML
を参照できないことでした。error
code