あなたはあなたに応答XMLをロードするために使用することができXDocumentと一緒にint.Parse
とint.TryParse
方法を見てみましょうことがあります。
var request = WebRequest.Create(...);
...
using (var response = request.GetResponse())
using (var stream = response.GetStream())
{
var doc = XDocument.Load(stream);
if (int.TryParse(doc.Root.Value, out value))
{
// the parsing was successful => you could do something with
// the integer value you have just read from the body of the response
// assuming the server returned the XML you have shown in your question,
// value should equal 427 here.
}
}
またはさらに簡単、XDocumentのLoadメソッドは、HTTPを理解し、
var doc = XDocument.Load("http://foo/bar");
if (int.TryParse(doc.Root.Value, out value))
{
// the parsing was successful => you could do something with
// the integer value you have just read from the body of the response
// assuming the server returned the XML you have shown in your question,
// value should equal 427 here.
}
このようにして、HTTPリクエスト/レスポンスを使用する必要はありません。 BCLによってすべてがあなたのために処理されます。これはちょっと大丈夫です。
文字列「427」を整数に変換する必要がありますか?それとも、リクエストしているページのステータスコードですか? –
ああ申し訳ありませんが、私の投稿から応答が取り除かれたことを認識しています。下記参照。 XSD intを取得する。 – TheITGuy