2011-11-23 67 views
0

,所以我有這個問題我真的想我的課序列化到如此地步,他們會是這樣的:XML序列化困擾

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

問題是我使用的第二個和第三個類的名稱是geting列爲標籤以及XML內。所以我的問題是:有沒有解決方法? 這是我的代碼。 類:

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

感謝您的幫助提前!

回答

1

[XmlArrayItem(typeof(order))]替換爲[XmlElement("order")][XmlArrayItem(typeof(progducts))][XmlElement("prod")]。這將在序列化列表時刪除一個級別。

+0

感謝隊友它工作得很好! – Jordan

0

您可以使用序列化屬性來更改要表示類結構的XML元素或屬性的名稱。見MSDN

+0

我不需要更改元素的名稱。我有2個額外的元素(第二個2類的名稱) – Jordan

+0

-1用於:沒有Seri​​alization屬性並且以不同的方式命名屬性,您將需要XmlElement或XmlAttribute屬性。 – Fischermaen

+0

我說的序列化屬性,不是xml屬性。請刪除-1。在.NET中,任何[someattributehere]命名的通用名稱都是'屬性'。如果您將其與XML屬性混淆,那不是我的問題。 – Maess

0

又一個屬性添加到您的財產順序是這樣的:

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

這應該工作。

+0

這隻會改變我的標籤的名稱。我需要的是由名稱類創建的標籤不被列出 – Jordan

+0

根據@Fischermaen,你應該得到-1來調用'屬性'....好吧,我明白了,當你是其他人的答案時會給出一個基本相同的答案GG。 – Maess

+0

@Maess:對不起,但XmlArray是一個屬性。 – Fischermaen

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>