Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8275297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:01:58+00:00 2026-06-08T08:01:58+00:00

I have a problem with adding an element to an XML file using C#.

  • 0

I have a problem with adding an element to an XML file using C#. I have my App.config files somewhere in my diff directory. So I am using LINQ to retrieve the values I want and to set the value from TextBoxes.

<appSettings>
    <add key="Something" value="false" />
    <add key="UserName" value="user0001" />
    <add key="Password" value="123456" />
    <add key="Environment" value="" />
    <add key="DBUserName" value="DBname23" />
    <add key="DBPassword" value="12345678" />
</appSettings>

The above is my XML file. I am able to retrieve the values of UserName and Password and set it with the encrypted ones. The way I am doing is shown below:

var doc1 = XDocument.Load(appConfigFile1);

var list1 = from appNode in doc1.Descendants("appSettings").Elements()
            where appNode.Attribute("key").Value == "UserName"
            select appNode;
var list2 = from appNode in doc1.Descendants("appSettings").Elements()
            where appNode.Attribute("key").Value == "Password"
            select appNode;
var list3 = from appNode in doc1.Descendants("appSettings").Elements()
            where appNode.Attribute("key").Value == "DBUserName"
            select appNode;
var list4 = from appNode in doc1.Descendants("appSettings").Elements()
            where appNode.Attribute("key").Value == "DBPassword"
            select appNode;
var element1 = list1.FirstOrDefault();
var element2 = list2.FirstOrDefault();
var element3 = list3.FirstOrDefault();
var element4 = list4.FirstOrDefault();
element1.Attribute("value").SetValue(txtbox1);
element2.Attribute("value").SetValue(txtbox2);
element3.Attribute("value").SetValue(txtbox3);
element4.Attribute("value").SetValue(txtbox4);
doc1.Save(appConfigFile1);

The requirements are such that if one of the elements from the XML file is deleted, I should be able to create a same element with key and value.

Example: Please compare the above xml with the below:

<appSettings>
    <add key="HasUI" value="false" />
    <add key="Password" value="123456" />
    <add key="Environment" value="" />
    <add key="DBUserName" value="DBname23" />
    <add key="DBPassword" value="12345678" />
</appSettings>

Above the element Username is missing. So how can I create an XML element like
<add key="UserName" value="" /> and set that to that same place in XML file?

The error I am getting when I load the XML file in C# is NullReferenceException.

Please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:02:00+00:00Added an answer on June 8, 2026 at 8:02 am

    For goodness sake, anything you do more than once should be a function!

    function UpdateOrCreateAppSetting(XMLDocument doc, string key, string value)
    {
        var list = from appNode in doc.Descendants("appSettings").Elements()
                where appNode.Attribute("key").Value == key
                select appNode;
        var e = list.FirstOrDefault();
    
        // If the element doesn't exist, create it
        if (e == null) {
            e = doc.CreateElement("add")
            e.Attributes.Append("key", key);
            e.Attributes.Append("value", value);
            doc.Descendants("appSettings").AppendChild(e);
    
        // If the element exists, just change its value
        } else {
            e.Attribute("value").SetValue(value);
        }
    }
    

    Now call the function four times and you’re good. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little problem with adding actions on UITabBarItems. I am not using
I have some problem with my .htaccess file. Here I am adding my problem.
I'm new to lisp and I have a problem when adding an element to
I have problem with adding class to every list element. This is example html:
I have problem on my webpage, I am adding an element in the document
I have the following problem: I am adding an element to a favorites list
I have problem adding arraylist to list view, will explain about my problem here..
I have a problem adding ADO.Net Entity Data Model to my existing project, or
I have a problem adding entities to a collection. public void SaveNotificationUsergroups(int bookingobjectID, int[]
I have a problem with adding OPTIONS to SELECT tag dynamically. Here is my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.