Solved

LINQ to XML to update an attribue value?

Posted on 2008-10-02
1
241 Views
Last Modified: 2013-11-11
I have this section in an xml file. I need to change the value for the AdminPassword.
What are the LINQ to XML statements I should use?



<configuration>
  <appSettings>
    <add key="AdminUsername" value="admin"/>
    <add key="AdminPassword" value="admin"/>
...
</appSettings>

Open in new window

0
Comment
Question by:jentel2
1 Comment
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 22631138
Hi jentel2;

Here is code snippet to show how to load the XML file and modify the AdminPassword value and save the results back to the file.

Fernando

using System.Xml.Linq;
 
namespace WindowsFormsApplication52
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            // File location of XML file
            String filename = "web.config";
            // Load the document into the XElement
            XElement webConfig = XElement.Load(filename, LoadOptions.PreserveWhitespace);
            // Find the key AdminPassword and change its password in the value field
            (from ele in webConfig.Element("appSettings").Elements("add")
                where ele.Attribute("key").Value == "AdminPassword"
                select ele).First().SetAttributeValue("value", textBox1.Text);
 
            // Save the XML file back to the system
            webConfig.Save(filename);
            Console.WriteLine("Hello");
        }
    }
}

Open in new window

0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question