Search code examples
c#xmlxml-serialization

xmldeserialization giving no result


I have this xml

<products>
  <product>
    <name>Demo</name>
  </product>
  <product>
    <name>Black Beauty III</name>
    <description>Beautiful les paul guitar</description>
    <company>
      <name>Les Paul</name>
      <description>Very Good Company for guitar</description>
      <year>1967</year>
      <address>
        <houseno>Flat no-52</houseno>
        <street>Avishkar Appt. JVLR, Jogeshwari(E)</street>
        <city>Mumbai</city>
        <country>India</country>
        <pin>400060</pin>
      </address>
    </company>
    <grossprice>5700</grossprice>
    <netprice>6000</netprice>
    <measure>1 pc</measure>
    <category>4</category>
  </product>
  <product>
    <name></name>
    <description></description>
    <company>
      <name></name>
      <description></description>
      <year></year>
      <address>
        <houseno></houseno>
        <street></street>
        <city></city>
        <country></country>
        <pin></pin>
      </address>
    </company>
    <grossprice></grossprice>
    <netprice></netprice>
    <measure></measure>
    <category></category>
  </product>
</products>

by using this code i cant deserialize it into object

using (FileStream fs = new FileStream(System.IO.Path.Combine(System.Environment.CurrentDirectory + "/Products.xml"), FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Products));

                    var products = serializer.Deserialize(fs) as Products;
                }

I have mentioned all the XML elements and XMLroots in classes like

[XmlType("products")]
[XmlInclude(typeof(Products))]
public class Products
{
    public Products() { }

    public Products(Boolean GetProducts) 
    {
        if (GetProducts)
            Items = GetAllProducts().Items;
    }

    [XmlElement("product")]
    public List<Product> Items { get; set; }

    public Products GetAllProducts()
    {
        try
        {
            Products allProducts = new Products();
            using (FileStream fs = new FileStream(System.IO.Path.Combine(System.Environment.CurrentDirectory + "/Products.xml"), FileMode.Open))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Products));

                var products = serializer.Deserialize(fs) as Products;
            }
            return allProducts;
        }
        catch (Exception ex) { return null; }
    }

    public Products GetAllProducts(ProductCategory Category)
    {
        return null;
    }
}

[XmlType("product")]
[XmlInclude(typeof(Product))]
public class Product
{
    [XmlElement("name")]
    String Name { get; set; }

    [XmlElement("description")]
    String Description { get; set; }

    [XmlElement("company")]
    Company Brand { get; set; }

    [XmlElement("grossprice")]
    String GrossPrice { get; set; }

    [XmlElement("netprice")]
    String NetPrice { get; set; }

    [XmlElement("measure")]
    String Measure { get; set; }

    [XmlElement("categoy")]
    ProductCategory Category { get; set; }

}

if u want i can post more class structure...


Solution

  • Make your properties public on your class. If they are private, protected, or internal, then the XmlSerializer cannot see then, and therefore cannot set them.