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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:52:35+00:00 2026-06-10T02:52:35+00:00

I am new to LINQ and using it to query XML (it’s incredible). I

  • 0

I am new to LINQ and using it to query XML (it’s incredible). I have the following function which simply creates a new List object for each list descendant found in the XML.:

public void ParseLists(XDocument xDoc)
{
    XNamespace ns = xDoc.Root.Name.Namespace;
    AddRange((
        from e in xDoc.Descendants(ns + "list")
        select new List
        {
            Id = (string)e.Element(ns + "id"),
            ItemsLink = (string)e.Element(ns + "items-link"),
            BatchLink = (string)e.Element(ns + "batch-link"),
            Name = (string)e.Element(ns + "name"),
            IsVendor = (string)e.Element(ns + "is-vendor"),
            Levels = (string)e.Element(ns + "levels")
        }).ToList());
}

And here’s a snippet of what the XML might look like:

<lists xmlns="www.namespaceurl.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <list>
    <batch-link>www.batchlinkurl.com</batch-link>
    <id>12345</id>
    . . .
  </list>
</lists>

The Question: When building the List objects, I initially used Convert.ToString() rather than (string) to get the elements values as a string. What I found was that:

Convert.ToString(e.Element(ns + "id")) will return <id>12345</id>

whereas

(string)e.Element(ns + "id") will return 12345.

Can someone explain why the return value is different?

  • 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-10T02:52:37+00:00Added an answer on June 10, 2026 at 2:52 am

    Convert.ToString(...) will call the overridden ToString() method, which returns the element itself as a string.

    (string) ... will use the overloaded explicit conversion to string operator, which returns the text contents of the element as a string.

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

Sidebar

Related Questions

I'm using LINQ-to-XML to query a file for a list of objects. They all
I have an xml file from which I am extracting html using LINQ to
I'm new to grails, but have previous experience using .net with c# and linq
I am developing asp.net mobile application. I am using LINQ to XML to query
I have a xml file on which I want to make a query to
Using LINQ to XML, XElements and XAttributes I'd like to filter a query using
I am trying to read an XML file using LINQ. I have had no
I am using Linq to NHibernate. I have a following test case : [TestMethod]
I am trying to query some XML data using Linq because it's easier than
I have a bunch of dropdownlists which I populate from an xml file using

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.