Read and write XML

Manojjupally
Posted by Manojjupally under C# category on | Points: 40 | Views : 1781
Code to read and write XML data


Read XML:


public static void ReadXML()
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlFilePath);
XmlElement xmlelem = xmldoc.DocumentElement;
XmlNodeList nodelst = xmldoc.SelectNodes("/Employees/Emp");
var tempnode = xmldoc.InnerText;
var tempnode1 = xmldoc.InnerXml;
XmlNodeList nodelst1 = xmlelem.ChildNodes;
}


Write XML


public static void WriteXML()
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlFilePath);

XmlNode node = xmldoc.CreateNode(XmlNodeType.Element, "EMP", "");
XmlNode IDElement = xmldoc.CreateElement("ID");
IDElement.InnerText = "5";
XmlNode nameElement = xmldoc.CreateElement("Name");
nameElement.InnerText = "Manoj";
XmlNode deptElement = xmldoc.CreateElement("Dept");
deptElement.InnerText = "30";
node.AppendChild(IDElement);
node.AppendChild(nameElement);
node.AppendChild(deptElement);
xmldoc.DocumentElement.AppendChild(node);
xmldoc.Save(xmlFilePath);
}

Comments or Responses

Login to post response