The xml file is having the following structure
<RootElement>
</RootElement>
i need to append the "Child" element to both RootElement and TestChild . For this I'm using the following code.
List<string> Str = new List<string> {"a","b"};
XmlDocument XDOC = new XmlDocument();
XDOC.Load(Application.StartupPath + "\\Sample.xml");
XmlNode RootNode = XDOC.SelectSingleNode("//RootElement");
XmlNode TestChild = XDOC.CreateNode(XmlNodeType.Element, "TestChild", null);
for (int Index = 0; Index < Str.Count; Index++)
{
XmlElement XEle = XDOC.CreateElement("Child");
XEle.SetAttribute("Name", Str[Index]);
TestChild.AppendChild(XEle);
RootNode.AppendChild(XEle);
}
RootNode.AppendChild(TestChild);
XDOC.Save(Application.StartupPath + "\\Sample.xml");
But with this i can append the child node to only the RootElement
The result should come like
<RootElement>
<Child Name="a"/>
<Child Name="b"/>
<TestChild>
<Child Name="a"/>
<Child Name="b"/>
</TestChild>
</RootElement>
But now i'm getting like
<RootElement>
<Child Name="a" />
<Child Name="b" />
<TestChild>
</TestChild>
</RootElement>
please give me a solution to do this
Thanks in advance