Web サービスから取得した結果を文字列として変換し、それをオブジェクトに変換しようとしています。
これは、サービスから取得している文字列です。
<StatusDocumentItem><DataUrl/><LastUpdated>2013-01-31T15:28:13.2847259Z</LastUpdated><Message>The processing of this task has started</Message><State>1</State><StateName>Started</StateName></StatusDocumentItem>
だから私はこれのためのクラスを持っています:
[XmlRoot]
public class StatusDocumentItem
{
[XmlElement]
public string DataUrl;
[XmlElement]
public string LastUpdated;
[XmlElement]
public string Message;
[XmlElement]
public int State;
[XmlElement]
public string StateName;
}
これは、XMLDeserializer を使用してその文字列を StatusDocumentItem 型のオブジェクトとして取得しようとしている方法です (注意: operationXML には文字列が含まれています)。
string operationXML = webRequest.getJSON(args[1], args[2], pollURL);
var serializer = new XmlSerializer(typeof(StatusDocumentItem));
StatusDocumentItem result;
using (TextReader reader = new StringReader(operationXML))
{
result = (StatusDocumentItem)serializer.Deserialize(reader);
}
Console.WriteLine(result.Message);
しかし、私の結果オブジェクトは常に空です。私は何を間違っていますか?
更新します。operationXML から取得した値はこのようなもので、デシリアライゼーションをブロックしている不要な xmlns 属性があります。その属性がなければ、すべてが正常に機能します。これがどのように見えるかです:
"<StatusDocumentItem xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><DataUrl/><LastUpdated>2013-02-01T12:35:29.9517061Z</LastUpdated><Message>Job put in queue</Message><State>0</State><StateName>Waiting to be processed</StateName></StatusDocumentItem>"