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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:29:24+00:00 2026-05-23T23:29:24+00:00

I am using Linq-to-Xml to read XML and update an existing data structure. Right

  • 0

I am using Linq-to-Xml to read XML and update an existing data structure. Right now I have the following code to do this:

        // Load all the test plan details
        var details = doc.Descendants()
                         .Select(x => new
                         {
                             Name = x.Attribute("name").ToStringValue(),
                             DbName = x.Attribute(DATABASE_ATTR).ToStringValue(),
                             Login = x.Attribute(USERNAME_ATTR).ToStringValue(),
                             Password = x.Attribute(PASSWORD_ATTR).ToStringValue(),
                             AppSource = x.Attribute(APPSOURCE_ATTR).ToStringValue()
                         })
                         .First();


        testPlan.Name = details.Name;
        testPlan.DatabaseName = details.DbName;
        testPlan.LoginUsername = details.Login;
        testPlan.LoginPassword = details.Password;
        testPlan.ApplicationSource = details.AppSource;
    }

This is kind of annoying to me because I have to create a temp variable and perform the data transfer. Is there any way for me to update the testPlan variable straight from within the Linq statement, which would cut down one step? I could not get it to work by adding the update code into the .Select() statement.

  • 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-05-23T23:29:26+00:00Added an answer on May 23, 2026 at 11:29 pm

    The following should work:

    doc.Descendants().First()
       .Select(x => 
               {
                   testPlan.Name= x.Attribute("name").ToStringValue();
                   testPlan.DatabaseName = x.Attribute(DATABASE_ATTR)
                                            .ToStringValue();
                   testPlan.LoginUsername = x.Attribute(USERNAME_ATTR)
                                             .ToStringValue();
                   testPlan.LoginPassword = x.Attribute(PASSWORD_ATTR)
                                             .ToStringValue();
                   testPlan.ApplicationSource = x.Attribute(APPSOURCE_ATTR)
                                                 .ToStringValue();
                   return testPlan;
               })
        .ToList();
    

    The call to First in the middle is to ensure, that only the attributes of the first descendant will be assigned to the properties of testPlan. The ToList at the end is used to really execute the code inside Select.

    Although this works, I don’t recommend using it, because it uses LINQ in a way it wasn’t created for. Furthermore, it’s easy to create it wrong:

    • If you forget the First, testPlan will contain the attribute values of the last element in your XML, because the select code is executed for each element and overwrites the properties.
    • If you forget the call to ToList or a similar method that forces the execution, the code inside Select will never be executed.

    So, basically, the problem is, that you create a lot of possibilities to introduce a bug.

    I would implement something like this in the following way:

    var x = doc.Descendants.First();
    testPlan.Name= x.Attribute("name").ToStringValue();
    testPlan.DatabaseName = x.Attribute(DATABASE_ATTR).ToStringValue();
    testPlan.LoginUsername = x.Attribute(USERNAME_ATTR).ToStringValue();
    testPlan.LoginPassword = x.Attribute(PASSWORD_ATTR).ToStringValue();
    testPlan.ApplicationSource = x.Attribute(APPSOURCE_ATTR).ToStringValue();
    

    This is shorter, easier to read and doesn’t violate LINQ to do something it wasn’t created for.

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

Sidebar

Related Questions

How to write binary data into XML using LINQ which is being read from
using LINQ to XML, this is a sample of my XML <shows> <Show Code=456
I am trying to read an XML file using LINQ. I have had no
I have the following code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
I want to read a xml file using Linq. This xml file is composed
I have an xml file that I am using linq-to-XML to read. Linq-to-XML is
I would like to read/write encrypted XML files using LINQ to XML. Does anyone
I'm using Linq-to-XML to do a simple is this user registered check (no security
I have an xml file from which I am extracting html using LINQ to
I am using the following XML structure <SERVERS> <SERVER NAME=A1 ID=1></SERVER> <SERVER NAME=A2></SERVER> <SERVER

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.