簡體   English   中英

如果使用linq-to-xml不存在對象的字段,如何不創建xml元素

[英]how not to create a xml element if a field of the object does not exist using linq-to-xml

我正在使用Linq to XML創建一個xml,並且值在Product對象中。 請參閱下面生成xml的c#代碼段:

new XElement(xn + "Products",
                    from p in products
                    select
                        new XElement(xn + "Product",
                            new XElement(xn + "ProductId", p.Id),
                            new XElement(xn + "Name", p.Name),
                            new XElement(xn + "Description", new XCData(p.LongDescription.StripHtml())),
                            new XElement(xn + "CategoryExternalId",
                                from c in categories
                                where c.Name == p.PrimaryCategory
                                select c.CategoryId),
                            new XElement(xn + "UPCs",
                                from s in p.SKU
                                select
                                    new XElement(xn + "UPC", s.UPC))))
                        ));

挑戰是UPC。 如果產品SKU陣列中沒有UPC條目,我不想創建UPCs xml節點。 上面的iepSKU在代碼片段中,是一個字符串UPC字段的數組。 因此,如果不存在單個UPC字段,即如果p.SKU.Count == 0 ,那么我根本不希望創建UPCs xml節點元素。

請參閱類模型代碼段:

public class Product
{
   public string Name { get; set; }
   public string Description { get; set; }
   public List<SKU> SKU { get; set; }
}

public class SKU
{
    public string UPC { get; set; }
    public string Name { get; set; }
    public string Overlap { get; set; }
    public string Productline { get; set; }
}

將其放在您的查詢中:

(p.SKU.Count > 0 ? 
    new XElement("UPCs",
        from s in p.SKU
        select new XElement( "UPC", s.UPC)) :
    null)

我創建了一個簡化版的查詢:

var xml = new XElement("Products",
                    from p in products
                    select
                        new XElement("Product",
                            new XElement("ProductId", p.Id),
                            new XElement("Name", p.Name),
                            (p.SKU.Count > 0 ? new XElement("UPCs",
                                                    from s in p.SKU
                                                    select new XElement("UPC", s.UPC))
                                                : null)));

對於那樣的輸入:

var products = new List<Product> {
    new Product {
        Id = 1,
        Name = "TestName",
        SKU = new List<SKU> {
            new SKU { Name = "test", UPC = "UPC1" },
            new SKU { Name = "test2", UPC = "UPC2" }
        }
    },
    new Product {
        Id = 1,
        Name = "TestName",
        SKU = new List<SKU> { }
    }
};

輸出是:

<Products>
  <Product>
    <ProductId>1</ProductId>
    <Name>TestName</Name>
    <UPCs>
      <UPC>UPC1</UPC>
      <UPC>UPC2</UPC>
    </UPCs>
  </Product>
  <Product>
    <ProductId>1</ProductId>
    <Name>TestName</Name>
  </Product>
</Products>

所以這正是你想要實現的,不是嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM