2014-09-05 69 views
2

我有以下XML意外的節點類型元素

<Envelope> 
<Body> 
    <RESULT> 
    <SUCCESS>TRUE</SUCCESS> 
    <RecipientId></RecipientId> 
    <ORGANIZATION_ID>12345-67890-b9e6bcd68d4fb511170ab3fcff55179d</ORGANIZATION_ID> 
    </RESULT> 
</Body> 
</Envelope> 

我敢嘗試反序列化到:

[XmlRoot(ElementName = "Envelope")] 
public class Add_Recipent_response 
{ 
    public string Body { get; set; } 
    public string RESULT { get; set; } 
    public string SUCCESS { get; set; } 
    public string RecipientId { get; set; } 
    public string ORGANIZATION_ID { get; set; } 
} 

使用這種方法:

protected void deserializeXML(string xmlResponse) 
{ 
    XmlSerializer deserializer = new XmlSerializer(typeof(Add_Recipent_response)); 
    using (TextReader reader = new StringReader(xmlResponse)) 
    { 
     try 
     { 
      Add_Recipent_response XmlData = (Add_Recipent_response)deserializer.Deserialize(reader); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.GetBaseException()); 
     } 
    } 
} 

這將引發例外:

InnerException = {"Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content. Line 4, position 2."}

誰能告訴我什麼,我做錯了什麼?

+0

東西,你可以使用Visual Studio的https://msdn.microsoft.com/en-us/library/hh371548(v=vs.110).aspx – suphero 2015-10-23 17:54:40

回答

7

Body和Result應該也是一個類,因爲它包含元素。像

[XmlRoot(ElementName = "Envelope")] 
public class Add_Recipent_response 
{ 
    public Body Body { get; set; } 
} 

public class Body 
{ 
    public Result RESULT { get; set; } 
} 

public class Result 
{ 
    public string SUCCESS { get; set; } 
    public string RecipientId { get; set; } 
    public string ORGANIZATION_ID { get; set; } 
} 
+0

由於生成的XML/JSON類 - 我新的序列化。我會試試這個...... -EDIT-剛剛嘗試過它,它很有用,非常感謝。我會盡快接受。 – Robert 2014-09-05 14:43:55

相關問題