Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected node type Element

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?

like image 563
Robert Avatar asked Sep 05 '14 14:09

Robert


People also ask

What is a node type in JavaScript?

Definition and Usage. The nodeType property returns the node type, as a number, of the specified node. If the node is an element node, the nodeType property will return 1.

What does the nodetype property return?

More "Try it Yourself" examples below. The nodeType property returns the node type, as a number, of the specified node. If the node is an element node, the nodeType property will return 1.

How to check if a node is an element or attribute?

If the node is an element node, the nodeType property will return 1. If the node is an attribute node, the nodeType property will return 2. If the node is a text node, the nodeType property will return 3.

How to check if a node type is read-only or not?

If the node is an attribute node, the nodeType property will return 2. If the node is a text node, the nodeType property will return 3. If the node is a comment node, the nodeType property will return 8. This property is read-only.


1 Answers

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; }
}
like image 154
venerik Avatar answered Sep 29 '22 21:09

venerik