2013-09-30 39 views
0

我只是在C#中開始編程,我有問題,系列化系列化C# - 對象集合

我想要做什麼:

<Products> 
    <ID>1</ID> 
    <Product> 
    <name>Samochod</name> 
    <price>20</price> 
    <mass>1000</mass> 
    </Products> 
<Products> 

我的想法:

[XmlRoot("Product")] 
    public class Product 
    { 

     [XmlElement("name")] 
     public string name { get; set; } 
     [XmlElement("price")] 
     public decimal price { get; set; } 
     [XmlElement("mass")] 
     public double mass { get; set; } 



     public static void serialization() 
     { 
      id iD = new id(); 
      List <id> idlist = new List<id>(); 
      List<Product> listprod = new List<Product>(); 
      idlist.Add(new id() {ID = 1}); 
      listprod.Add(new Product() { name = "Samochod", price = 20, mass = 1000 }); 

      XmlRootAttribute root = new XmlRootAttribute("Products"); 
      TextWriter textwriter = new StreamWriter(@"C:\Products.xml"); 
      XmlSerializer xmlserializer = new XmlSerializer(typeof(List<Product>),root); 
      xmlserializer.Serialize(textwriter, listprod); 
      textwriter.Close(); 


     } 


    } 

    [XmlRoot("ID")] 
    public class id:Product 
    { 
     [XmlElement("ID")] 
     public int ID { get; set; } 
    } 

不幸的是,在編譯過程中出錯。那麼你能給我一些提示/指導如何解決這個問題嗎?

對不起,沒有錯誤。現在,它看起來像:

<Products> 
     <Product> 
     <ID>0</ID> 
     <name>Samochod</name> 
     <price>20</price> 
     <mass>1000</mass> 
     </Products> 
<Products> 

但是,當我改變了這一行:

idlist.Add(new id() {ID = 1}); 

這樣:

listprod.Add(new id() {ID = 1}); 

然後,我有一個錯誤:

There was an error generating the XML document. 
+6

你得到具體什麼錯誤? –

+1

這對我來說正好適合生產你想要的XML文件。您應該提供有關您獲得的錯誤的更多詳細信息。 –

+0

剛剛編譯並運行良好(雖然與你想要的略有不同xml)。我不得不改變路徑,因爲我沒有權限訪問我機器上的C:\文件夾。根據賈斯汀的評論 - 你得到了什麼錯誤? –

回答

0

您的idlist最終未被使用。由於ID是真正的Product的屬性附加傷害,我會建議這簡化這樣的代碼:

[XmlRoot("Product")] 
public class Product 
{ 
    [XmlElement("ID")] 
    public int ID { get; set; } 

    [XmlElement("name")] 
    public string name { get; set; } 
    [XmlElement("price")] 
    public decimal price { get; set; } 
    [XmlElement("mass")] 
    public double mass { get; set; } 



    public static void serialization() 
    { 
     List<Product> listprod = new List<Product>(); 
     listprod.Add(new Product() { ID = 1, name = "Samochod", price = 20, mass = 1000 }); 

     XmlRootAttribute root = new XmlRootAttribute("Products"); 
     TextWriter textwriter = new StreamWriter(@"C:\temp\Products.xml"); 
     XmlSerializer xmlserializer = new XmlSerializer(typeof(List<Product>), root); 
     xmlserializer.Serialize(textwriter, listprod); 
     textwriter.Close(); 
    } 
} 

那麼你應該得到

<?xml version="1.0" encoding="utf-8"?> 
<Products xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <Product> 
    <ID>1</ID> 
    <name>Samochod</name> 
    <price>20</price> 
    <mass>1000</mass> 
    </Product> 
</Products> 
+0

但我的任務是讓產品標籤的外部列表 – enarg

+0

然後,您應該將其添加到同一個集合('listprod'),因爲您的ID類是從Product類派生的。 從技術上講,您可以這樣做,但這不會是「好」的XML - 您的ID和產品節點出現在同一級別,並且本質上屬於同一類型。不是一個id通常是產品的屬性? –

+0

因此,看起來像這樣: id iD = new id(); List listprod = new List (); listprod.Add(new id(){ID = 1}); listprod.Add(new Product(){name =「Samochod」,price = 20,mass = 1000}); –