2013-02-01 10 views
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; 
} 

そして、これは私がそれを取得しようとしている方法ですXMLDeserializerとタイプStatusDocumentItemの対象として文字列(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>" 
+1

"operationXMLには文字列が含まれています" - それはありますか?あなたは実際にデバッガなどと実際にチェックしましたか? "getJSON"はXMLを検索するために怪しいです。 –

+1

xmlの例を_operationXML_に設定した場合。デシリアライゼーションは完璧に機能します。 –

+0

はい、それは文字列を含んでいます、私はデバッガから得たものです: " 2013-02-01T12: 13:02.0997071Zこのタスクの処理が開始を開始しました」Cé[email protected] – Disasterkid

答えて

50

はこれを試してみてください:

string xml = "<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>"; 
var serializer = new XmlSerializer(typeof(StatusDocumentItem)); 
StatusDocumentItem result; 

using (TextReader reader = new StringReader(xml)) 
{ 
    result = (StatusDocumentItem)serializer.Deserialize(reader); 
} 

Console.WriteLine(result.Message); 
Console.ReadKey(); 

それは "仕事をキューに入れ" と表示していますか?

関連する問題