问题
I've looked at several similar SO questions but still can't solve my issue.
Using the following code I try to deserialize:
private static QCOrderInfo GetOrderFromXml(XDocument xmlDoc){
XmlSerializer serializer = new XmlSerializer(typeof(QCOrderInfo));
var post =
(QCOrderInfo)serializer.Deserialize(xmlDoc.Root.CreateReader());
return post ?? new QCOrderInfo();
}
The error resulting:
System.Xml.XmlException: ReadElementContentAs() methods cannot be called on an element that has child elements.
at System.Xml.XmlReader.SetupReadElementContentAsXxx(String methodName)
Too much code to post but view working dotnetfiddle: https://dotnetfiddle.net/pRCWQh
回答1:
Your appoved appraisers are not getting deserialized properly.
So add a class
[Serializable]
public class ApprovedAppraisers
{
[XmlElement(ElementName = "Appraiser")]
public string Appraiser { get; set; }
}
And change
public string[] ApprovedAppraisers { get; set; }
to
public ApprovedAppraisers[] ApprovedAppraisers { get; set; }
来源:https://stackoverflow.com/questions/57380238/c-sharp-deserialize-xml-error-readelementcontentas-methods-cannot-be-called-on