Search code examples
c#xmlxmlserializerxmlreadernamecheap

Namecheap API: XML reader exception in C#


I am having a problem while deserializing an XML:

[XmlRoot("ProductCategory")]
public class ProductCategory
{
    public Product[] Products;
}

public class Product
{
    [XmlArray("Product")]
    [XmlArrayItem("ProductPrice", typeof(ProductPrice))]
    public ProductPrice[] Prices;
}

public class ProductPrice
{
    [XmlAttribute]
    public int Duration;

    [XmlAttribute]
    public string DurationType;

    [XmlAttribute]
    public decimal Price;

    [XmlAttribute]
    public decimal RegularPrice;

    [XmlAttribute]
    public decimal YourPrice;

    [XmlAttribute]
    public string CouponPrice;

    [XmlAttribute]
    public string Currency;
}

and this is the action:

public ProductType GetPricing()
        {
            XDocument doc = new Query(_params)
              .AddParameter("ProductType", "DOMAIN")
              .AddParameter("ActionName","REGISTER")
              .Execute("namecheap.users.getPricing");

            var serializer = new XmlSerializer(typeof(ProductType), _ns.NamespaceName);

            using (var reader = doc.Root.Element(_ns + "CommandResponse").Element(_ns + "ProductType").CreateReader())
            {
                return (ProductType)serializer.Deserialize(reader);
            }
        }

I am getting this error: NullReferenceException: Object reference not set to an instance of an object.

And here you can find example of the xml: https://www.namecheap.com/support/api/methods/users/get-pricing.aspx

Any ideas?


Solution

  • Found number of issues with your code. Here is the working code

    First you will have to have the objects defined as below

    [XmlRoot("ProductType", Namespace = "http://api.namecheap.com/xml.response")]
    public class ProductType
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
    
        [XmlElement("ProductCategory")]
        public ProductCategory[] ProductCategories;
    }
    public class ProductCategory
    {
        [XmlElement("Product")]
        public Product[] Products;
    }
    
    public class Product
    {
        [XmlElement("Price")]
        public ProductPrice[] Prices;
    }
    
    public class ProductPrice
    {
        [XmlAttribute]
        public int Duration;
    
        [XmlAttribute]
        public string DurationType;
    
        [XmlAttribute]
        public decimal Price;
    
        [XmlAttribute]
        public decimal RegularPrice;
    
        [XmlAttribute]
        public decimal YourPrice;
    
        [XmlAttribute]
        public string CouponPrice;
    
        [XmlAttribute]
        public string Currency;
    }
    

    Here is the code to deserialize it

    var serializerx = new XmlSerializer(typeof(ProductType), "http://api.namecheap.com/xml.response");
    XElement doc = XElement.Load("sample1.xml");
    XNamespace ns = doc.Name.Namespace;
    var e = doc.Element(ns + "CommandResponse").Element(ns + "UserGetPricingResult").Element(ns + "ProductType");
    
    using (var reader = e.CreateReader())
    {
        var prod =  (ProductType)serializerx.Deserialize(reader);
    }
    

    Hope this helps