Unexpected node type Element

送分小仙女□ 提交于 2021-01-27 08:34:36

问题


I have the following XML:

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

Which I'm trying to deserialize to:

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

With this method:

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

This throws an exception:

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

Can anyone tell me what I'm doing wrong?


回答1:


Body and Result should be a classes as well because it contains elements. Something like

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


来源:https://stackoverflow.com/questions/25688396/unexpected-node-type-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!