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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:55:28+00:00 2026-05-11T08:55:28+00:00

I can select the first customer node and change its company name with the

  • 0

I can select the first customer node and change its company name with the code below.

But how do I select customer node where ID=2?

    XDocument xmldoc = new XDocument(         new XDeclaration('1.0', 'utf-8', 'yes'),         new XComment('These are all the customers transfered from the database.'),         new XElement('Customers',             new XElement('Customer',                 new XAttribute('ID', 1),                 new XElement('FullName', 'Jim Tester'),                 new XElement('Title', 'Developer'),                 new XElement('Company', 'Apple Inc.')                 ),             new XElement('Customer',                 new XAttribute('ID', 2),                 new XElement('FullName', 'John Testly'),                 new XElement('Title', 'Tester'),                 new XElement('Company', 'Google')                 )             )         );      XElement elementToChange = xmldoc.Element('Customers').Element('Customer').Element('Company');     elementToChange.ReplaceWith(new XElement('Company', 'new company value...')); 

ANSWER:

Thanks guys, for the record, here is the exact syntax to search out the company element in the customer-with-id-2 element, and then change only the value of the company element:

XElement elementToChange = xmldoc.Element('Customers')     .Elements('Customer')     .Single(x => (int)x.Attribute('ID') == 2)     .Element('Company'); elementToChange.ReplaceWith(     new XElement('Company', 'new company value...')     ); 

ANSWER WITH METHOD SYNTAX:

Just figured it out in method syntax as well:

XElement elementToChange = (from c in xmldoc.Element('Customers')                                 .Elements('Customer')                             where (int)c.Attribute('ID') == 3                             select c).Single().Element('Company'); 
  • 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. 2026-05-11T08:55:29+00:00Added an answer on May 11, 2026 at 8:55 am

    Assuming the ID is unique:

    var result = xmldoc.Element('Customers')                    .Elements('Customer')                    .Single(x => (int?)x.Attribute('ID') == 2); 

    You could also use First, FirstOrDefault, SingleOrDefault or Where, instead of Single for different circumstances.

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

Sidebar

Related Questions

Not sure if you can do this, but I want to select the first
In code-behind I can do this to select something: // Select item in first
is this good code? can it be simplified somehow? SELECT u.id,u.title,u.title,u.first,u.last FROM (((tblusers u
var customer= from cust in customerData select new Customer { CustomerID = cust[id], Name
I can select the first item in the div using $('.class:first') Now since I
How can I select the first 4 rows of a data.frame : Weight Response
Given the following POCO Code First Entities public class Customer { public int CustomerId
I can select all the distinct values in a column in the following ways:
You can select a part of a web page with the mouse. I know
Can a JApplet use a JFileChooser so that the user can select a file

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.