C# 使用foreach循环和XmlNodeList C将新节点追加到节点列表#
目前我处理的是XML的类型: 关于XML文件,我想检查一个节点,如果没有找到该节点,我必须将该节点附加到该文件中。我尝试了以下代码:C# 使用foreach循环和XmlNodeList C将新节点追加到节点列表#,c#,xml,foreach,appendchild,xmlnodelist,C#,Xml,Foreach,Appendchild,Xmlnodelist,目前我处理的是XML的类型: 关于XML文件,我想检查一个节点,如果没有找到该节点,我必须将该节点附加到该文件中。我尝试了以下代码: private void button12_Click(object sender, EventArgs e) { // XmlNodeList func_name_value = doc.GetElementsByTagName("FUNCTION-NAME-VALUE"); XmlNodeList list_def_re
private void button12_Click(object sender, EventArgs e)
{
// XmlNodeList func_name_value = doc.GetElementsByTagName("FUNCTION-NAME-VALUE");
XmlNodeList list_def_ref = doc.GetElementsByTagName("DEFINITION-REF");
foreach (XmlNode nodeDef in list_def_ref)
{
if (nodeDef.InnerText == "/AUTOSAR/Com/ComConfig/ComSignal")
{
if (nodeDef.ParentNode.HasChildNodes)
{
XmlNodeList list = nodeDef.ParentNode.ChildNodes;
foreach (XmlNode node in list)
{
if (node.Name == "PARAMETER-VALUES")
{
XmlNodeList param_list = node.ChildNodes;
foreach (XmlNode paramNode in param_list)
{
if (paramNode.Name == "FUNCTION-NAME-VALUE")
{
XmlNodeList func_child_list = paramNode.ChildNodes;
foreach (XmlNode funChild in func_child_list)
{
if (funChild.Name == "DEFINITION-REF")
{
string tout = "/AUTOSAR/Com/ComConfig/ComSignal/ComTimeoutNotification";
string comnotify = "/AUTOSAR/Com/ComConfig/ComSignal/ComNotification";
string invalid = "/AUTOSAR/Com/ComConfig/ComSignal/ComInvalidNotification";
if (funChild.InnerText != tout)
{
if (funChild.InnerText != comnotify)
{
//ADD ComInvalidNotification tags
XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
paramNode.AppendChild(newNode);
XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
XmlAttribute attr = doc.CreateAttribute("DEST");
attr.Value = "FUNCTION-NAME-DEF";
defRefNode.Attributes.SetNamedItem(attr);
newNode.AppendChild(defRefNode);
XmlNode val = doc.CreateElement("VALUE");
val.InnerText = "ComInvalidNotification";//ComInvalidNotification + shortName ;
newNode.AppendChild(val);
}
else
{
//ADD ComNotification tags
XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
paramNode.AppendChild(newNode);
XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
XmlAttribute attr = doc.CreateAttribute("DEST");
attr.Value = "FUNCTION-NAME-DEF";
defRefNode.Attributes.SetNamedItem(attr);
newNode.AppendChild(defRefNode);
XmlNode val = doc.CreateElement("VALUE");
val.InnerText = "ComNotification Node";//ComNotification + shortName;
newNode.AppendChild(val);
}
}
else
{
//ADD ComTimeOutNotification tags
XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
paramNode.AppendChild(newNode);
XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
XmlAttribute attr = doc.CreateAttribute("DEST");
attr.Value = "FUNCTION-NAME-DEF";
defRefNode.Attributes.SetNamedItem(attr);
newNode.AppendChild(defRefNode);
XmlNode val = doc.CreateElement("VALUE");
val.InnerText = "ComTimeoutNotification node";//ComInvalidNotification + shortName;
newNode.AppendChild(val);
}
}
}
}
}
}
}
}
}
}
doc.Save(openFileDialog1.FileName);
我得到的错误是:元素列表已更改。枚举操作无法继续。
在第一次执行foreach循环后,我得到了这个错误,我应该如何克服这个错误?这里有两个问题:
XmlNodeList
集合。Microsoft的标准做法是,在迭代过程中修改集合时引发异常,例如,请参阅以下文档:
例外情况
InvalidOperationException
:创建枚举数后修改了集合
为了避免此异常,您可以通过XmlNodeList
手动使用for
循环和索引,或者
和
的XML节点,但也正在创建具有此名称的节点。这意味着您在迭代早期创建的节点可能会在迭代后期找到,从而导致递归创建更多节点。我想你不想要这个。如果我的计算是正确的,您应该快照所有满足搜索条件的节点,然后迭代快照,如下所示:
private static void AddMissingNodes(XmlDocument doc)
{
var query = from nodeDef in doc.GetElementsByTagName("DEFINITION-REF").Cast<XmlNode>()
where nodeDef.InnerText == "/AUTOSAR/Com/ComConfig/ComSignal"
from nodeDefSibling in nodeDef.ParentNode.ChildNodes.Cast<XmlNode>()
where nodeDefSibling.Name == "PARAMETER-VALUES"
from paramNode in nodeDefSibling.ChildNodes.Cast<XmlNode>()
where paramNode.Name == "FUNCTION-NAME-VALUE"
select new
{
paramNode = paramNode,
func_child_list = (from funChild in paramNode.ChildNodes.Cast<XmlNode>()
where funChild.Name == "DEFINITION-REF"
select funChild).ToList() // Snapshot func_child_list by calling ToList()
};
foreach (var paramNodeAndFuncChildren in query.ToList()) // Snapshot everything by calling ToList()
foreach (var funChild in paramNodeAndFuncChildren.func_child_list)
AddMissingNodes(doc, paramNodeAndFuncChildren.paramNode, funChild);
}
private static void AddMissingNodes(XmlDocument doc, XmlNode paramNode, XmlNode funChild)
{
string tout = "/AUTOSAR/Com/ComConfig/ComSignal/ComTimeoutNotification";
string comnotify = "/AUTOSAR/Com/ComConfig/ComSignal/ComNotification";
string invalid = "/AUTOSAR/Com/ComConfig/ComSignal/ComInvalidNotification";
if (funChild.InnerText != tout)
{
if (funChild.InnerText != comnotify)
{
//ADD ComInvalidNotification tags
XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
paramNode.AppendChild(newNode);
XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
XmlAttribute attr = doc.CreateAttribute("DEST");
attr.Value = "FUNCTION-NAME-DEF";
defRefNode.Attributes.SetNamedItem(attr);
newNode.AppendChild(defRefNode);
XmlNode val = doc.CreateElement("VALUE");
val.InnerText = "ComInvalidNotification";//ComInvalidNotification + shortName ;
newNode.AppendChild(val);
}
else
{
//ADD ComNotification tags
XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
paramNode.AppendChild(newNode);
XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
XmlAttribute attr = doc.CreateAttribute("DEST");
attr.Value = "FUNCTION-NAME-DEF";
defRefNode.Attributes.SetNamedItem(attr);
newNode.AppendChild(defRefNode);
XmlNode val = doc.CreateElement("VALUE");
val.InnerText = "ComNotification Node";//ComNotification + shortName;
newNode.AppendChild(val);
}
}
else
{
//ADD ComTimeOutNotification tags
XmlNode newNode = doc.CreateElement("FUNCTION-NAME-VALUE");
paramNode.AppendChild(newNode);
XmlNode defRefNode = doc.CreateElement("DEFINITION-REF");
XmlAttribute attr = doc.CreateAttribute("DEST");
attr.Value = "FUNCTION-NAME-DEF";
defRefNode.Attributes.SetNamedItem(attr);
newNode.AppendChild(defRefNode);
XmlNode val = doc.CreateElement("VALUE");
val.InnerText = "ComTimeoutNotification node";//ComInvalidNotification + shortName;
newNode.AppendChild(val);
}
}
我看到342个节点被添加到XmlDocument
此错误表示代码在枚举集合时试图修改集合,这是不允许的。尝试使用
for
而不是foreach
@Hari Prasad您的意思是,我必须将所有foreach循环更改为for代码中的所有迭代?我以前尝试过将第一个foreach循环更改为for,但错误仍然相同!是的,至少在您看到这些错误的地方是这样的;在按钮12中,虽然这次没有错误,但我看不到XML中有任何更改file@ShreedharHegde-你记得把文件存回去吗?使用您提供的XML文件,我看到342个节点被添加到XmlDocument
。我尝试了使用for循环的传统方法,现在我可以在相应的位置添加相应的节点,我应该如何为新创建的节点设置名称空间?我还没有在代码中声明任何名称空间,但我的XML已经有了名称空间。我想将XML的现有namspace设置为新创建的节点。你能帮我一下吗?现在,这就是添加新节点的方式。我希望它在XML中可见,但它应该具有默认的XMLnamespace@ShreedharHegde-你可以试试。但是,stackoverflow问题的首选格式是,因此如果您需要其他无关的帮助,您可能希望提出新问题。非常感谢您的帮助:)
var fileName = @"D:\Temp\Question36740480\autosar_ecucvalues_Fx4_L.xml";
var newFileName = @"D:\Temp\Question36740480\autosar_ecucvalues_Fx4_L_NEW.xml";
var doc = new XmlDocument();
doc.Load(fileName);
int countBefore = doc.SelectNodes("descendant::*").Count;
AddMissingNodes(doc);
int countAfter = doc.SelectNodes("descendant::*").Count;
Debug.WriteLine(string.Format("Added {0} nodes", countAfter - countBefore));
doc.Save(newFileName);
Debug.WriteLine("Wrote: " + newFileName);