2012-12-11 39 views
7

मेरे पास दूसरी सूची (वेरिएंट के साथ एक उत्पाद) के अंदर एक सूची है। मैं अभिभावक सूची को उस पर सेट विशेषताएँ रखना चाहूंगा (केवल id और name)।गुणों के साथ एक सूची का एक्सएमएल क्रमबद्धता

वांछित आउटपुट

<embellishments> 
    <type id="1" name="bar bar foo"> 
     <row> 
      <id>1</id> 
      <name>foo bar</name> 
      <cost>10</cost> 
     </row>  
    </type> 
</embellishments> 

वर्तमान कोड

[XmlRoot(ElementName = "embellishments", IsNullable = false)] 
public class EmbellishmentGroup 
{ 
    [XmlArray(ElementName="type")] 
    [XmlArrayItem("row", Type=typeof(Product))] 
    public List<Product> List { get; set; } 

    public EmbellishmentGroup() { 
     List = new List<Product>(); 
     List.Add(new Product() { Id = 1, Name = "foo bar", Cost = 10m }); 
    } 
} 

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

    [XmlElement("name")] 
    public string Name { get; set; } 

    [XmlElement("cost")] 
    public decimal Cost { get; set; } 
} 

वर्तमान आउटपुट

0,123,
<embellishments> 
    <type> 
     <row> 
      <id>1</id> 
      <name>foo bar</name> 
      <cost>10</cost> 
     </row> 
    </type> 
</embellishments> 

उत्तर

9

आप किसी अन्य वर्ग जो type तत्व का प्रतिनिधित्व करता है बनाने की जरूरत है। फिर आप गुणों के लिए इसमें गुण जोड़ सकते हैं, जैसे:

[XmlRoot(ElementName = "embellishments", IsNullable = false)] 
public class EmbellishmentGroup 
{ 
    [XmlElement("type")] 
    public MyType Type { get; set; } 

    public EmbellishmentGroup() 
    { 
     Type = new MyType(); 
    } 
} 

public class MyType 
{ 
    [XmlAttribute("id")] 
    public int Id { get; set; } 

    [XmlAttribute("name")] 
    public string Name { get; set; } 

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

    public MyType() 
    { 
     Id = 1; 
     Name = "bar bar foo"; 
     List = new List<Product>(); 
     Product p = new Product(); 
     p.Id = 1; 
     p.Name = "foo bar"; 
     p.Cost = 10m; 
     List.Add(p); 
    } 
} 

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

    [XmlElement("name")] 
    public string Name { get; set; } 

    [XmlElement("cost")] 
    public decimal Cost { get; set; } 
} 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^