意外的节点类型元素 [英] Unexpected node type Element

查看:28
本文介绍了意外的节点类型元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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 = {"意外的节点类型元素.ReadElementString方法只能在具有简单或空内容的元素上调用.第 4 行,位置 2."}

谁能告诉我我做错了什么?

Can anyone tell me what I'm doing wrong?

推荐答案

Body 和 Result 也应该是一个类,因为它包含元素.类似的东西

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

这篇关于意外的节点类型元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆