XML序列化的麻烦

时间:2011-11-23 14:37:17

标签: c# serialization

所以我有这个问题我试图将我的类序列化到他们看起来像这样的程度:

    <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>

但无论我尝试如何最终都是这样的:

     <?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>

问题是我使用的第二和第三类的名称是在xml中列为标签的geting。所以我的问题是:有什么方法可以解决这个问题吗? 这是我的代码。 类:

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; }
    }

}

这是执行代码:

        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();

提前感谢您的帮助!

4 个答案:

答案 0 :(得分:1)

[XmlArrayItem(typeof(order))]替换为[XmlElement("order")],将[XmlArrayItem(typeof(progducts))]替换为[XmlElement("prod")]。这将在序列化列表时删除一个级别。

答案 1 :(得分:0)

您可以使用序列化属性更改要表示类结构的XML元素或属性的名称。见MSDN

答案 2 :(得分:0)

只需在您的属性订单中添加其他属性,如下所示:

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

这应该有效。

答案 3 :(得分:0)

如果使用以下使用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;
        }
    }
}

然后是以下代码:

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();

提供以下输出:

<?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>