I looked around the internet but I can't find a solution for my problem, although I guess this should be very simple.
I have a XML document. There two nodes that look like:
<Attachments>
</Attachments>
<Templates>
</Templates>
After adding two elements to each node, they should look like:
<Attachments>
<Attachment INDEX0="Test1" />
<Attachment INDEX1="Test2" />
</Attachments>
<Templates>
<Template INDEX0="Test1">EMPTY</Template>
<Template INDEX0="Test2">EMPTY</Template>
</Templates>
I tried following code for the first one:
XmlDocument doc = new XmlDocument();
doc.Load(Path.Combine(Directory.GetCurrentDirectory(), "test.xml"));
XmlElement root = doc.DocumentElement;
XmlNode node = root.SelectSingleNode("//Attachments");
List<String> list = new List<string>() {"Test1","Test2"};
foreach(var item in list)
{
XmlElement elem = doc.CreateElement("Attachment");
root.AppendChild(elem);
XmlNode subNode = root.SelectSingleNode("Attachment");
XmlAttribute xKey = doc.CreateAttribute(string.Format("INDEX{0}", list.IndexOf(item).ToString()));
xKey.Value = item;
subNode.Attributes.Append(xKey);
}
but this does absolutely nothing. How can I achieve these two cases?
Thank you!