2012-09-06 16 views
5

Ho una struttura di dati come sottoCome aggiungere altro attributo in XElement?

class BasketCondition 
{ 
     public List<Sku> SkuList { get; set; } 
     public string InnerBoolean { get; set; } 
} 

class Sku 
{ 
     public string SkuName { get; set; } 
     public int Quantity { get; set; } 
     public int PurchaseType { get; set; } 
} 

Ora dobbiamo popoliamo qualche valore ad esso

var skuList = new List<Sku>(); 
skuList.Add(new Sku { SkuName = "TSBECE-AA", Quantity = 2, PurchaseType = 3 }); 
skuList.Add(new Sku { SkuName = "TSEECE-AA", Quantity = 5, PurchaseType = 3 }); 

BasketCondition bc = new BasketCondition(); 
bc.InnerBoolean = "OR"; 
bc.SkuList = skuList; 

L'uscita desiderio è

<BasketCondition> 
    <InnerBoolean Type="OR"> 
     <SKUs Sku="TSBECE-AA" Quantity="2" PurchaseType="3"/> 
     <SKUs Sku="TSEECE-AA" Quantity="5" PurchaseType="3"/> 
    </InnerBoolean> 
</BasketCondition> 

Il mio programma finora è

XDocument doc = 
     new XDocument(
     new XElement("BasketCondition", 

     new XElement("InnerBoolean", new XAttribute("Type", bc.InnerBoolean), 
     bc.SkuList.Select(x => new XElement("SKUs", new XAttribute("Sku", x.SkuName))) 
     ))); 

che mi dà l'output come

<BasketCondition> 
    <InnerBoolean Type="OR"> 
    <SKUs Sku="TSBECE-AA" /> 
    <SKUs Sku="TSEECE-AA" /> 
    </InnerBoolean> 
</BasketCondition> 

Come posso aggiungere il resto degli attributi quantità e PurchaseType al mio programma.

Si prega di aiutare

risposta

8

ho trovato

bc.SkuList.Select(x => new XElement("SKUs", new XAttribute("Sku", x.SkuName), 
              new XAttribute("Quantity", x.Quantity), 
              new XAttribute("PurchaseType", x.PurchaseType) 
            )) 
4

si può semplicemente fare questo:

yourXElement.Add(new XAttribute("Quantity", "2")); 
yourXElement.Add(new XAttribute("PurchaseType", "3")); 
Problemi correlati