我正在构建xml文件:
public YandexMarketDoc()
{
var root = _doc.CreateElement("yml_catalog");
root.SetAttribute("date", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
_doc.AppendChild(root);
var shop = _doc.CreateElement("shop");
root.AppendChild(shop);
Categories = _doc.CreateElement("categories");
shop.AppendChild(Categories);
Items = _doc.CreateElement("offers");
shop.AppendChild(Items);
}
比我添加节点到类别节点:
public override void AddCategory(string title, int id, int parent)
{
var i = Categories.OwnerDocument.CreateElement("category");
i.SetAttribute("id", id.ToString());
if(parent > 0)
i.SetAttribute("parentId", parent.ToString());
i.InnerText = title;
Categories.AppendChild(i);
}
但是类别节点中的结果节点会更改其位置。这是将节点插入Category:
的方法static void FillTemplates(PriceList doc, List<PriceAgregator> price)
{
foreach (PriceAgregator i in price.Where(r=> r.Level == 0))
{
foreach (PriceAgregator level1 in price.Where(r => r.Level == 1 && r.ParentID == i.ID))
{
AddCategory(doc, i.Title, level1.ParentID);
foreach (PriceAgregator pa in price.Where(r => r.Level == 2 && r.ParentID == level1.ID))
{
int p = 0;
foreach (PriceAgregatorItem item in pa.Childs)
{
if (AddItem(doc, item))
p++;
}
if (p == 0)
{
RemoveCategory(doc, i.ID);
}
else
{
AddCategory(doc, pa.Title, pa.ID, level1.ParentID);
}
}
}
}
}
我认为首先插入必须从笔画类别: AddCategory(doc,i.Title,level1.ParentID);