XmlDocument 是其节点的工厂,因此您必须这样做:
XmlNode newNode = document.CreateNode(XmlNodeType.Element, "product", "");
或者它的快捷方式:
XmlNode newNode = document.CreateElement("product");
然后将新创建的节点添加到其父节点:
node.ParentNode.AppendChild(newNode);
如果必须处理添加的节点,则必须明确执行:节点列表是匹配搜索条件的节点的快照,则不会动态更新。只需调用insertIntoTable() 即可添加节点:
insertIntoTable(node.ParentNode.AppendChild(newNode));
如果您的代码有很大不同,您可能需要进行一些重构以使此过程成为一个两步批处理(首先搜索要添加的节点,然后将它们全部处理)。当然,您甚至可以采用完全不同的方法(例如,将节点从 XmlNodeList 复制到列表,然后将它们添加到两个列表中)。
假设这已经足够了(并且您不需要重构)让我们把所有东西放在一起:
foreach (var node in productsXml.SelectNodes("/portfolio/products/product"))
{
if (node.Attributes["name"].InnertText.StartsWith("PB_"))
{
XmlNode newNode = document.CreateElement("product");
insertIntoTable(node.ParentNode.AppendChild(newNode));
}
// Move this before previous IF in case it must be processed
// before added node
insertIntoTable(node);
}
重构
重构时间(如果你有一个 200 行的函数,你真的需要它,比我在这里展示的要多得多)。第一种方法,即使效率不高:
var list = productsXml
.SelectNodes("/portfolio/products/product")
.Cast<XmlNode>();
.Where(x.Attributes["name"].InnertText.StartsWith("PB_"));
foreach (var node in list)
node.ParentNode.AppendChild(document.CreateElement("product"));
foreach (var node in productsXml.SelectNodes("/portfolio/products/product"))
insertIntoTable(node); // Or your real code
如果您不喜欢两次通过的方法,您可以像这样使用ToList():
var list = productsXml
.SelectNodes("/portfolio/products/product")
.Cast<XmlNode>()
.ToList();
for (int i=0; i < list.Count; ++i)
{
var node = list[i];
if (node.Attributes["name"].InnertText.StartsWith("PB_"))
list.Add(node.ParentNode.AppendChild(document.CreateElement("product"))));
insertIntoTable(node);
}
请注意,在第二个示例中,必须使用 for 而不是 foreach,因为您在循环中更改了集合。请注意,您甚至可以保留原来的 XmlNodeList 对象...