Jordan
Jordan

Reputation: 530

XML serialization troubles

so i have this problem i`m trying to serialize my classes to the point that they will look like this:

    <orders>
       <order>
           <ordersID>22070</ordersID>
           <ordersTotal>53.00</ordersTotal>
                <prod>
                    <productCount>1</productCount>
                    <productPrice>2.00</productPrice>
                    <productPricePromo>0.00</productPricePromo>
                    <productDiscount>0</productDiscount>
                    <productName>Шампоан против косопад Loreal Density Advanced 500 мл.</productName>
                    <productNumber>30055</productNumber>
                </prod>
                <prod>
                    <productCount>1</productCount>
                    <productPrice>6.00</productPrice>
                    <productPricePromo>0.00</productPricePromo>
                    <productDiscount>0</productDiscount>
                    <productName>Маска за суха коса Loreal Интенс Рипер 200 мл.</productName>
                    <productNumber>30107</productNumber>
                 </prod>
     </order>
 </orders>

But whatever i try e end up like this:

     <?xml version="1.0" encoding="UTF-8"?>
     <orders xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <order>
       <order>
         <ordersID>0</ordersID>
         <ordersTotal>0</ordersTotal>
         <products> 
           <progducts> 
           <productCount>0</productCount>
           <productPrice>0</productPrice>
           <productPricePromo>0</productPricePromo>
           <productDiscount>0</productDiscount>
           <productNumber>0</productNumber>
       </progducts>
       <progducts>
           <productCount>0</productCount>
           <productPrice>0</productPrice> 
           <productPricePromo>0</productPricePromo> 
           <productDiscount>0</productDiscount>
           <productNumber>0</productNumber> 
       </progducts>
     </products> 
   </order>
  </order> 
</orders>

The problem is the names of the second and third class i`m using is geting listed as tags aswell inside the xml. So my question is: is there any way around this? Here is my code aswell. Classes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace testXML
{
    [Serializable]
    public  class orders 
    {
        private List <order> m_order = new List <order>();

        [XmlArrayItem(typeof(order))]
        public List<order> order
        {
            get { return m_order; }
            set { m_order = value; }
        }

    }

    [Serializable]
    public class order
    {
        public int ordersID         { get; set; }
        public double ordersTotal   { get; set; }
        private List<progducts> prod = new List<progducts>();

        [XmlArrayItem(typeof(progducts))]
        public List<progducts> products
        {
            get { return prod; }
            set { prod = value; }
        }

    }

    [Serializable]
    public class progducts 
    {
        public string productName       { get; set; }
        public int productCount         { get; set; }
        public double productPrice      { get; set; }
        public double productPricePromo { get; set; }
        public double productDiscount   { get; set; }
        public Int64 productNumber      { get; set; }
    }

}

And here is the execution code:

        orders f = new orders();
        order or = new order();
        progducts p1 = new progducts();
        progducts p2 = new progducts();



        f.order.Add(or);
        or.products.Add(p1);
        or.products.Add(p2);




        XmlSerializer xmlSerializer = new XmlSerializer(typeof(orders));
        TextWriter writer = new StreamWriter("Family.xml");
        xmlSerializer.Serialize(writer, f);
        writer.Close();

Thank you for any help in advance!

Upvotes: 0

Views: 293

Answers (4)

erikH
erikH

Reputation: 2346

Replace the [XmlArrayItem(typeof(order))] with [XmlElement("order")] and [XmlArrayItem(typeof(progducts))] with [XmlElement("prod")]. That will remove one level when serializing the lists.

Upvotes: 1

Maess
Maess

Reputation: 4146

You can use a serialization attribute to change the names of the XML elements or attributes you want to represent your class structure. See MSDN

Upvotes: 0

tom redfern
tom redfern

Reputation: 31750

If you use the following classes which were generated using xsd.exe:

using System.Xml.Serialization;
using System;

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class orders 
{
    private ordersOrder orderField;

    public ordersOrder order 
    {
        get 
        {
            return this.orderField;
        }
        set 
        {
            this.orderField = value;
        }
    }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class ordersOrder 
{

    private int ordersIDField;
    private double ordersTotalField;
    private ordersOrderProd[] prodField;

    public int ordersID 
    {
        get 
        {
            return this.ordersIDField;
        }
        set 
        {
            this.ordersIDField = value;
        }
    }

    public double ordersTotal 
    {
        get 
        {
            return this.ordersTotalField;
        }
        set 
        {
            this.ordersTotalField = value;
        }
    }

    [System.Xml.Serialization.XmlElementAttribute("prod")]
    public ordersOrderProd[] prod 
    {
        get 
        {
            return this.prodField;
        }
        set 
        {
            this.prodField = value;
        }
    }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class ordersOrderProd 
{
    private int productCountField;
    private double productPriceField;
    private double productPricePromoField;
    private double productDiscountField;
    private string productNameField;
    private Int64 productNumberField;

    public int productCount 
    {
        get 
        {
            return this.productCountField;
        }
        set 
        {
            this.productCountField = value;
        }
    }

    public double productPrice 
    {
        get 
        {
            return this.productPriceField;
        }
        set 
        {
            this.productPriceField = value;
        }
    }

    public double productPricePromo 
    {
        get 
        {
            return this.productPricePromoField;
        }
        set 
        {
            this.productPricePromoField = value;
        }
    }

    public double productDiscount 
    {
        get 
        {
            return this.productDiscountField;
        }
        set 
        {
            this.productDiscountField = value;
        }
    }

    public string productName 
    {
        get 
        {
            return this.productNameField;
        }
        set 
        {
            this.productNameField = value;
        }
    }

    public Int64 productNumber 
    {
        get 
        {
            return this.productNumberField;
        }
        set 
        {
            this.productNumberField = value;
        }
    }
}

Then the following code:

var orders = new orders
{
    order = new ordersOrder
    {
        ordersID = 1,
        ordersTotal = 1,
        prod = new ordersOrderProd[]
        {  
            new ordersOrderProd
            {
                productCount = 1, 
                productDiscount = 8.4, 
                productName = "Widget", 
                productNumber = 987987, 
                productPrice = 78.9, 
                productPricePromo = 68.75
            }
        }
    }
};

XmlSerializer xmlSerializer = new XmlSerializer(typeof(orders));
TextWriter writer = new StreamWriter(".\\Family.xml");
xmlSerializer.Serialize(writer, orders);
writer.Close();

Gives you the following output:

<?xml version="1.0" encoding="utf-8"?>
<orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <order>
    <ordersID>1</ordersID>
    <ordersTotal>1</ordersTotal>
    <prod>
      <productCount>1</productCount>
      <productPrice>78.9</productPrice>
      <productPricePromo>68.75</productPricePromo>
      <productDiscount>8.4</productDiscount>
      <productName>Widget</productName>
      <productNumber>987987</productNumber>
    </prod>
  </order>
</orders>

Upvotes: 0

Fischermaen
Fischermaen

Reputation: 12458

Just add another attributes to your property order like this:

 [XmlArray("orders")]
 [XmlArrayItem("order", typeof(order))]  
 public List<order> order         
 {             
     get { return m_order; }             
     set { m_order = value; }         
 } 

That should work.

Upvotes: 0

Related Questions