Reputation: 61
Trying to deserialize this XML to C# but unable to find the cause of the error.
<PaymentMethod xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance" d2p1:type="GenericPaymentMethod">
<Amount>158.83</Amount>
<CurrencyCode>None</CurrencyCode>
<CurrencyCodeAlpha>USD</CurrencyCodeAlpha>
<AccountNumber>************1001</AccountNumber>
<UtcExpiration>2023-06-30T23:59:59</UtcExpiration>
<PaymentMethodSubtype>MC</PaymentMethodSubtype>
<AccountHolderName>John Doe</AccountHolderName>
<QuotedAmount>158.83</QuotedAmount>
<QuotedCurrencyCode>None</QuotedCurrencyCode>
<QuotedCurrencyCodeAlpha>USD</QuotedCurrencyCodeAlpha>
<AccountNumberId>24</AccountNumberId>
<TokenId>3174119969340463143</TokenId>
</PaymentMethod>
[Serializable, XmlRoot(ElementName = "PaymentMethod")]
public class PaymentMethod
{
[XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "d2p1", Namespace = "http://www.w3.org/2000/xmlns/")]
public string D2p1 { get; set; }
[XmlElement(ElementName = "Amount")]
public string Amount { get; set; }
[XmlElement(ElementName = "CurrencyCode")]
public string CurrencyCode { get; set; }
[XmlElement(ElementName = "CurrencyCodeAlpha")]
public string CurrencyCodeAlpha { get; set; }
[XmlElement(ElementName = "AccountNumber")]
public string AccountNumber { get; set; }
[XmlElement(ElementName = "UtcExpiration")]
public string UtcExpiration { get; set; }
[XmlElement(ElementName = "PaymentMethodSubtype")]
public string PaymentMethodSubtype { get; set; }
[XmlElement(ElementName = "AccountHolderName")]
public string AccountHolderName { get; set; }
[XmlElement(ElementName = "QuotedAmount")]
public string QuotedAmount { get; set; }
[XmlElement(ElementName = "QuotedCurrencyCode")]
public string QuotedCurrencyCode { get; set; }
[XmlElement(ElementName = "QuotedCurrencyCodeAlpha")]
public string QuotedCurrencyCodeAlpha { get; set; }
[XmlElement(ElementName = "AccountNumberId")]
public string AccountNumberId { get; set; }
[XmlElement(ElementName = "TokenId")]
public string TokenId { get; set; }
}
Error: InvalidOperationException: The specified type was not recognized: name='GenericPaymentMethod', namespace='', at .
Upvotes: 0
Views: 632
Reputation: 34421
Here is solution :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication33
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(PaymentMethod));
PaymentMethod paymentMethod = (PaymentMethod)serializer.Deserialize(reader);
}
}
[XmlInclude(typeof(GenericPaymentMethod))]
public class PaymentMethod
{
}
[Serializable, XmlRoot(ElementName = "PaymentMethod")]
public class GenericPaymentMethod : PaymentMethod
{
[XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "d2p1", Namespace = "http://www.w3.org/2000/xmlns/")]
public string D2p1 { get; set; }
[XmlElement(ElementName = "Amount")]
public string Amount { get; set; }
[XmlElement(ElementName = "CurrencyCode")]
public string CurrencyCode { get; set; }
[XmlElement(ElementName = "CurrencyCodeAlpha")]
public string CurrencyCodeAlpha { get; set; }
[XmlElement(ElementName = "AccountNumber")]
public string AccountNumber { get; set; }
[XmlElement(ElementName = "UtcExpiration")]
public string UtcExpiration { get; set; }
[XmlElement(ElementName = "PaymentMethodSubtype")]
public string PaymentMethodSubtype { get; set; }
[XmlElement(ElementName = "AccountHolderName")]
public string AccountHolderName { get; set; }
[XmlElement(ElementName = "QuotedAmount")]
public string QuotedAmount { get; set; }
[XmlElement(ElementName = "QuotedCurrencyCode")]
public string QuotedCurrencyCode { get; set; }
[XmlElement(ElementName = "QuotedCurrencyCodeAlpha")]
public string QuotedCurrencyCodeAlpha { get; set; }
[XmlElement(ElementName = "AccountNumberId")]
public string AccountNumberId { get; set; }
[XmlElement(ElementName = "TokenId")]
public string TokenId { get; set; }
}
}
Upvotes: 2