Reputation: 544
I have a XML structure like below:
<buttons>
<button>
<text>Yes</text>
<type>Submit</type>
</button>
<button>
<text>No</text>
<type>Cancel</type>
</button>
</buttons>
I have de-serialization classes as follows:
[XmlRoot("PageData")]
public class PageData
{
[XmlArray("buttons")]
[XmlArrayItem("button")]
public List<Button> Buttons { get; set; }
}
public class Button
{
[XmlElement("text")]
public string Text { get; set; }
[XmlElement("type"))]
public PANELBUTTONTYPE Type { get; set; }
}
public enum PANELBUTTONTYPE
{
[XmlEnum(Name = "Submit")]
Submit,
[XmlEnum(Name = "Cancel")]
Cancel,
}
When I am deserializing the data, I am getting bellow error
{"There was an error reflecting property 'Buttons'."}
Upvotes: 1
Views: 401
Reputation: 1063328
Firstly, I expect that if you did look recursively through the exceptions you'll find what you need. The output from errors is actually very detailed. I can't get it to error in the way you describe, which suggests the example you show is not the same as your real code. However, let's look at the problems (hopefully this will help show you how to fix it), using:
string s = @"<buttons>
<button>
<text>Yes</text>
<type>Submit</type>
</button>
<button>
<text>No</text>
<type>Cancel</type>
</button>
</buttons>";
try
{
var serializer = new XmlSerializer(typeof(PageData));
var obj = (PageData)serializer.Deserialize(new StringReader(s));
}
catch (Exception ex)
{
while (ex != null)
{
Console.Error.WriteLine(ex.Message);
ex = ex.InnerException;
}
}
We get:
There is an error in XML document (1, 2).
<buttons xmlns=''> was not expected.
True: your xml starts at <buttons>
, but you have said the root is <PageData>
. We can fix that:
[XmlRoot("buttons")]
public class PageData
{
[XmlElement("button")]
public List<Button> Buttons { get; set; }
}
With that changed - we try again, and it works fine.
Upvotes: 3