Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

XML-Adding Node using InsertAfter()

Program:
using System;
using System.Xml;
namespace DOM
{
internal class Program
{
static void Main(string[] args)
{
string tempXml = @"<Colleges>
<college id='2116' name='REC'/>
<college id='2117' name='RIT'/>
<college id='2118' name='RSA'/>
<college id='2119' name='RGCE'/></Colleges>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(tempXml);
XmlElement xmlEle = xmlDoc.CreateElement("College");
XmlAttribute xmlAttribute = xmlDoc.CreateAttribute("id");
xmlAttribute.Value = "2120";
xmlEle.Attributes.Append(xmlAttribute);
XmlAttribute xmlAttribute2 = xmlDoc.CreateAttribute("name");
xmlAttribute2.Value = "RGCE";
xmlEle.Attributes.Append(xmlAttribute2);
XmlNode xmlNode = xmlDoc.DocumentElement;
xmlNode.InsertAfter(xmlEle, xmlNode.LastChild);
xmlDoc.Save(Console.Out);
xmlDoc.Save("C:\\xml files\\first.xml");
Console.ReadKey();
}}}
Output:

You might also like