0

I need to create xml element in runtime in the existing xml file. How do i do that in c#? For example, there is a xml file as

<data value = "1">
 <user>one</user>
</data>

I want to create a element in runtime and output should be like this,

 <data value = "1">
   <user>one</user>
   <status>used</status>
 </data>
  • 6
    What have you tried already? There is a myriad of ways to do this in C# as well as a number of questions that are already answered on Stack Overflow dealing with this exact issue :-) . – Martin Zikmund May 22 '18 at 14:40
  • Possible duplicate of [How to modify existing XML file with XmlDocument and XmlNode in C#](https://stackoverflow.com/questions/2558787/how-to-modify-existing-xml-file-with-xmldocument-and-xmlnode-in-c-sharp) – Foitn May 22 '18 at 14:50
  • Possible duplicate of [How can I build XML in C#?](https://stackoverflow.com/questions/284324/how-can-i-build-xml-in-c) – Magnetron May 22 '18 at 15:31

1 Answers1

0

Use AooendChild() to add new child node. Try Something like following

XmlDocument doc = new XmlDocument();

XmlNode root = doc.DocumentElement;

//Create a new node.
XmlElement elem = doc.CreateElement("Status");
elem.InnerText="used";

//Add the node to the document.
root.AppendChild(elem);