Solved

LINQ to XML to update an attribue value?

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now