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 8698509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:44:43+00:00 2026-06-13T01:44:43+00:00

How can I change part of a ConnectionString with LINQ to XML . For

  • 0

How can I change part of a ConnectionString with LINQ to XML. For example, I only want to change the Data Source and Initial Catalog values in the following:

  <connectionStrings>
    <add name="connStr" connectionString="Data Source=data-source;Initial Catalog=db;User ID=user;Password=pass" providerName="System.Data.SqlClient;MultipleActiveResultSets=true;" />
  </connectionStrings>

I understand how to change the whole connectionString attribute, but I don’t want to do that, I only want to change certain parts.

I am using the following code to modify parts of a connection string and for the most part it works, but I how can I modify it so it does not remove the attribute/values that are not separated by ;, providerName in this case.

string[] connArr = connectionString.Value.Split(';');

            for (int i = 0; i < connArr.Length; i++)
            {
                //Get the attribute and the value splitted by the "="
                string[] arrSubConn = connArr[i].Split('=');

                if (arrSubConn.Length == 2)
                {
                    string connAttr = arrSubConn[0];
                    string connVal = arrSubConn[1];

                    //Change the server name
                    if (connAttr == "Data Source")
                        connVal = environmentSettings.Server;


                    //Change the database name
                    if (connAttr == "Initial Catalog")
                        connVal = environmentSettings.Database;

                    newConnString += String.Format("{0}={1};", connAttr, connVal);
                } 
            }
  • 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-13T01:44:44+00:00Added an answer on June 13, 2026 at 1:44 am

    LINQ to XML will only be able to recover the actual connectionString attribute for you. Manipulation of the actual string element is beyond LINQ to XML’s capabilities. If you want to change the string itself you are going to need to manipulate it with other functions. A quick way to do that might be to split the string on = and ;. You know that every element with an even index is a key and every odd index element is a value. You can then search for the Data Source and Initial Catalog elements and modify the values in the next array locations.

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

Sidebar

Related Questions

how can I change the grey part into white color? I want my tabcontrol
How can I change a color of some part of treenode Text ? For
how can i change the size of (particular one)cell which is the part of
I can change SQL at runtime. Can I do the same with LINQ ?
According to http://www.rebol.com/docs/core23/rebolcore-15.html You can use change/part to parse and replace but that doesn't
I have a CSHTML file on the server. can I change part of the
My Question: Is it possible to change only part of a standard theme of
I am using JXTaskPane component. I want to know whether one can change the
the aim of my project: user can change the color of a part to
I have the following url. http://domain.com/userfiles/dynamic/images/whatever_dollar_1318105152.png Everything in the url can change except the

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.