Solved

LINQ to XML to update an attribue value?

Posted on 2008-10-02
1
242 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.NET Enums [Flags] & Bitwise Design Question 6 39
itextsharp with c# 3 18
ASP.NET data base connection 35 45
Web page design problem 3 19
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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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