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

Ask A Question

Stats

  • Questions 238k
  • Answers 238k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer urldecode the string, do the formatting, and then urlencode it… May 13, 2026 at 6:46 am
  • Editorial Team
    Editorial Team added an answer For a start, you have an Enabled property on both… May 13, 2026 at 6:46 am
  • Editorial Team
    Editorial Team added an answer Your ListView will fill your MyUserControl2 - if you want… May 13, 2026 at 6:46 am

Related Questions

I have 3 tables which look like this: Location Node Sektor ----- ------- -------
Lets say I have the following tables: Customers Products CustomerProducts Is there a way
I think this has to be THE most frustrating thing I've ever done in
I am binding the dropdown with db entity. ddlCustomer.DataSource = Customer.GetAll(); ddlCustomer.DataTextField = CustomerName;

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.