Modify XML file with tagname in c#.net


You can modify a xml file with a tag name using below simple code snippet. 

XML File


<?xml version="1.0" encoding="utf-8"?>
<Books>
  <book>
    <title>Sleep Research</title>
    <pages>300</pages>
    <price>100</price>
  </book>
  <book>
    <title>Sleep and Memory</title>
    <pages>400</pages>
    <price>200</price>
  </book>
  <book>
    <title>Parenting Guide</title>
    <pages>500</pages>
    <price>300</price>
  </book>
</Books>


C# Program to Modify above xml file accessing using a tag name



using System;

//C# Program to modify xml file
//Access the tag with tagname and modify the value.
namespace ProgramCall
{
    class Program
    {
        static void Main(string[] args)
        {
            //Make sure xml file and Application exe are in same directory
            //If you are running in debug mode in visual studio, xml file should be present in debug directory.
            string filePath="XMLFile1.xml";

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(filePath);
           
            System.Xml.XmlNodeList elemList = doc.GetElementsByTagName("title");

            if (elemList != null && elemList.Count > 0)
            {
                elemList[0].InnerText = "Sleep Research";
                elemList[1].InnerText = "Sleep and Memory";
                elemList[2].InnerText = "Parenting Guide";
            }
            doc.Save(filePath);
        }
    }
}

Zooming in Winforms Webbrowser Control in .NET
Filtering Datatable with Select Conditions and Sorting in C#.NET
Hiding a property from displaying in Datagridview in C#.NET
Logic to detect if Application is started from Citrix/Terminal Server/PC in C#
Winforms Application.DoEvents Method
Read Machine IP Address in C#
Check if IIS is running using C#
Implementing IEnumerable in C#
Built in Delegates in .NET Framework
Changing Winform webbrowser control IE version in C#.NET
Set the dropdown width of any combobox based on the data
Difference between Const and ReadOnly
Example for Property Change Notification in C#.NET
Example Program using Delegates and Events in C#
AutoComplete in .NET Winforms TextBox
How to read data from XML String and insert in to table in SQL Server
WebBrowser.navigate Data Submission by Get or Post Methods
Datagridview Paging
Copy Datagridview cell value using ContextMenu
Inserting data in Datatable in C#
Cookie in .NET
Using Control's Child Index and Dock property for achieving desired layout in a form.
Accessing section group inside configuration of web.config
Create File in .NET C#
Change button shape in .net
Dispose vs Finalize in .net




modify xml file with tagname in c#.net, c# program to modify xml file