Solved

LINQ to XML to update an attribue value?

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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