意外的节点类型Element

本文关键字:Element 类型 节点 意外 | 更新日期: 2023-09-27 18:20:51

我有以下XML:

<Envelope>
 <Body>
  <RESULT>
   <SUCCESS>TRUE</SUCCESS>
   <RecipientId>9876543210</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."}

有人能告诉我我做错了什么吗?

意外的节点类型Element

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; }
}