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の場合、UPC 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; }
}