我有一组对象需要迭代并将数据拉出来创建XML文件。我正在尝试使用XML Linq这样做,但似乎我没有抓住这个概念。这是我的代码:
string jSonProducts = File.ReadAllText(settings.productJsonConfig.JSONProductFilePath);
ICollection<ProductSearchModel> prods = null;
prods = JsonConvert.DeserializeObject<ICollection<ProductSearchModel>>(jSonProducts);
foreach (ProductSearchModel prod in prods)
{
var xmlNode =
new XElement("Feed",
new XAttribute("xmlns", settings.bvXMLConfig.xmlns),
new XAttribute("name", settings.bvXMLConfig.xmlName),
new XAttribute("incrmental", settings.bvXMLConfig.xmlIncremental),
new XAttribute("extractDate",DateTime.UtcNow),
new XElement("Products"),
new XElement("Product"),
new XElement("ExternalId", prod.SKU),
new XElement("Name", prod.Description.Name),
new XElement("Description", prod.Description.Description),
new XElement("BrandExternalID",prod.Properties.Brand.FeedName),
new XElement("CategoryExternalId"), //look up the category here by sku
new XElement("ModelNumbers"),
new XElement("ModelNumber",prod.SKU),
new XElement("ManufacturingPartNumbers"),
new XElement("ManufacturingPartNumber",prod.SKU),
new XElement("UPCs"),
new XElement("UPC", prod.UPC),
new XElement("Attributes"),
new XElement("Attribute"),
new XAttribute("id","BV_FE_FAMILY"),
new XElement("Value")
);
}
我正在尝试创建以下XML:
<?xml version="1.0" encoding="utf-8"?>
<Feed xmlns="http://www.bazaarvoice.com/xs/PRR/ProductFeed/14.7" name="LifetimeProducts" incremental="false" extractDate="2017-10-20T13:21:41">
<Products>
<Product>
<ExternalId>12345</ExternalId>
<Name>Product 1</Name>
<Description>
<![CDATA[Proudct Description]]></Description>
<BrandExternalId>Brandx</BrandExternalId>
<CategoryExternalId>Category</CategoryExternalId>
<ProductPageUrl><![CDATA[http://something.com]]></ProductPageUrl>
<ImageUrl>http://image.com</ImageUrl>
<ModelNumbers>
<ModelNumber>12345</ModelNumber>
</ModelNumbers>
<ManufacturerPartNumbers>
<ManufacturerPartNumber>12345</ManufacturerPartNumber>
</ManufacturerPartNumbers>
<Attributes>
<Attribute id="BV_FE_FAMILY">
<Value>Family</Value>
</Attribute>
<Attribute id="BV_FE_FAMILY">
<Value>Family2</Value>
</Attribute>
<Attribute id="BV_FE_FAMILY">
<Value>Family 3</Value>
</Attribute>
</Attributes>
</Product>
</Products>
我只需要一个feed节点(作为root),然后在这个节点下面有产品节点。这似乎不起作用,这是我第一次真正使用LINQ和XML。代码似乎是将数据拉入节点,但是对于foreach循环,我认为我做错了。我需要调整什么才能得到这个?
感谢。
答案 0 :(得分:0)
在C#中,通过将参数放在括号内来将参数传递给方法。例如,这不会起作用:
double sqrt = Math.Sqrt(),
1234;
你这样做:
double sqrt = Math.Sqrt(1234);
您还尝试为集合中的每个项目重新创建新的根节点。相反,您需要一个根节点,其中包含多个子节点。
所以你的代码应该看起来像这样。请记住,我没有设置您的ProductSearchModel
课程,所以我还没有对此进行测试。我依赖于这样的假设,即你的缩进至少反映了你的意图,并且元素的名称(ModelNumbers / ModelNumber等)似乎证实了这一点。
乍一看,循环的内容看起来与您的代码类似,但请密切关注括号。它们在世界上发挥了重要作用。
var xFeed =
new XElement("Feed",
new XAttribute("xmlns", settings.bvXMLConfig.xmlns),
new XAttribute("name", settings.bvXMLConfig.xmlName),
new XAttribute("incrmental", settings.bvXMLConfig.xmlIncremental),
new XAttribute("extractDate", DateTime.UtcNow)
);
var xProducts = new XElement("Products");
xFeed.Add(xProducts);
foreach (ProductSearchModel prod in prods)
{
var xProduct =
new XElement("Product",
new XElement("ExternalId", prod.SKU),
new XElement("Name", prod.Description.Name),
new XElement("Description", prod.Description.Description),
new XElement("BrandExternalID", prod.Properties.Brand.FeedName),
new XElement("CategoryExternalId"), //look up the category here by sku
new XElement("ModelNumbers",
new XElement("ModelNumber", prod.SKU)),
new XElement("ManufacturingPartNumbers",
new XElement("ManufacturingPartNumber", prod.SKU)),
new XElement("UPCs",
new XElement("UPC", prod.UPC)),
new XElement("Attributes"),
new XElement("Attribute",
new XAttribute("id", "BV_FE_FAMILY"),
new XElement("Value"))
);
xProducts.Add(xProduct);
}