Wednesday, January 11, 2017

Read and write Xml using C#


          var xdoc = new XmlDocument();
            xdoc.Load(webConfigPath);
            var connStringnodes = xdoc.SelectSingleNode("/configuration/connectionStrings");

            if (connStringnodes != null)
            {
                if (connStringnodes.ChildNodes.Count > 0)
                {
                    var elementNodeList = xdoc.GetElementsByTagName("connectionStrings");
                    foreach (XmlNode node in elementNodeList)
                    {
                        while (node.FirstChild != null)
                            node.RemoveChild(node.FirstChild);
                    }
                }
                var addElement = xdoc.CreateElement("add");
                addElement.SetAttribute("name", "conStringname");
                addElement.SetAttribute("connectionString", connectionStringValue);
                addElement.SetAttribute("providerName", providerNameValue);
                connStringnodes.AppendChild(addElement.Clone());

            }
            xdoc.Save(webConfigPath);

Above code will Change the Connectionstring child elements... 

No comments:

Post a Comment

Comments

Protected by Copyscape Plagiarism Software