0

現在、ASP.net Web API を使用して Web サービスを実装していますが、メソッドの 1 つが文字列を返します。問題は、次のような文字列を返すことです。

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization">Some Resource</string>

この種の応答が必要ですが、Web サービス クライアントで逆シリアル化する方法がわかりません。

文字列またはプリミティブ データ型を表す xml をどのようにデシリアライズしますか?

ありがとう!

4

2 に答える 2

2

System.Net.Http.Formatting.dllからReadAsAsyncを使用できます。「uri」と言うと、このデータが取得されます。

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
  Some Resource
</string>

次に、ReadAsAsyncを使用してXMLの文字列を取得できます。

        HttpClient client = new HttpClient();
        var resp = client.GetAsync(uri).Result;
        string value = resp.Content.ReadAsAsync<string>().Result;

(ここでReadAsAsync <>の使用法を示すために、.Resultを直接呼び出しています...)

于 2012-10-17T17:23:12.807 に答える
0
// Convert the raw data into a Stream
string rawData = "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">Some Resource</string>";
MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(rawData)); 

// User DataContractSerializer to deserialize it
DataContractSerializer serializer = new DataContractSerializer(typeof(string));
string data = (string)serializer.ReadObject(stream);

Console.WriteLine(data);
于 2012-10-17T15:39:29.623 に答える