19
我試圖將我從Web服務獲得的結果轉換爲字符串並將其轉換爲對象。從字符串中反序列化XML
這是我從我的服務獲取字符串:
<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;
}
這是如何我試圖讓字符串類型StatusDocumentItem的對象與XMLDeserializer(NB 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>"
「operationXML包含字符串」 - 是嗎?你真的用一個調試器檢查過嗎? 「getJSON」檢索XML看起來很腥。 –
如果您將xml示例設置爲_operationXML_。反序列化非常好。 –
是的,它確實包含字符串,下面是我從調試器得到的內容:「 2013-02-01T12: 13:02.0997071Z LASTUPDATED>這個任務的處理已經開始 開始 StatusDocumentItem>」 –
Disasterkid