How to Add config file xml node using C#

Posted by senthil | 11:26:00 AM | | 0 comments »

This is code Add xml node in configuration file using C# method. Step:1 this is sample.config file

<TABLE>
<ROW>
    <COLUMN SOURCE="1" NAME="age" />
    <COLUMN SOURCE="3" NAME="firstname" />
    <COLUMN SOURCE="4" NAME="lastname" />
  </ROW>
</TABLE>
Step:2 I want to add xml node for my application configuration settings. so i want to add configuration setting using user interface Step:3 I created user interface one add page for add xml node. Step:4 I used this method and pass the xml value add the node.
public bool AddconfigFileXmlNode()
        {
            try
            {
                var doc = new XmlDocument();
                doc.Load("D:\\sample.config");
                 
                XmlNode profile;                
                profile = doc.SelectSingleNode("TABLE/ROW");

                XmlElement newChild = doc.CreateElement("COLUMN");

                newChild.SetAttribute("SOURCE", "4");
                newChild.SetAttribute("NAME", "lastname");  
            

                profile.AppendChild(newChild);
                doc.Save("D:\\sample.config");
            }
            catch { }
            return true;
        }

0 comments

Related Posts Plugin for WordPress, Blogger...