在xml表达式中使用循环foreach
本文关键字:循环 foreach xml 表达式 | 更新日期: 2023-09-27 17:53:31
帮帮我吧!我需要创建xml文件。我将xml文件中的元素从文本文件中分离出来(config screenos juniper)。我想取关键词,也就是节点和它的元素。
我想实现这样的输出(xml文件):
<VR1>
<...>
</VR1>
<VR2>
<...>
</VR2>
<VR3>
<...>
</VR3>
但是我只有这样的输出:
<VR3>
<...>
</VR3>
这样的代码:
foreach (var match in myCollection)
{
StringWriter stringwriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringwriter);
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement(match);
;
...
...
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndDocument();
XmlDocument docSave = new XmlDocument();
docSave.LoadXml(stringwriter.ToString());
//write the path where you want to save the Xml file
docSave.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() +"Roting.xml");
}
这里myCollection包含:VR1,VR2,VR3。很明显,我使用loop foreach是错误的,但我不明白如何在这种情况下正确使用它。
StringWriter stringwriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringwriter);
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();
foreach (var match in myCollection)
{
xmlTextWriter.WriteStartElement(match);
;
...
...
xmlTextWriter.WriteEndElement();
}
xmlTextWriter.WriteEndDocument();
XmlDocument docSave = new XmlDocument();
docSave.LoadXml(stringwriter.ToString());
//write the path where you want to save the Xml file
docSave.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() +"Roting.xml");