Solved

LINQ to XML to update an attribue value?

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

19 Experts available now in Live!

Get 1:1 Help Now