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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:37:47+00:00 2026-06-15T22:37:47+00:00

I am using LINQ to convert from XML file to a class that I

  • 0

I am using LINQ to convert from XML file to a class that I have created. no matter what I am trying I am getting 0 in the count of the list.

List<UpdateMember> updateMembers = new List<ExeTeam.UpdateMember>();
updateMembers = GetMember(doc);
try
{
  IEnumerable<UpdateMember> member = from r in doc.Descendants("UpdateMember").Descendants("member")
  select new UpdateMember()
  {
    Birthdate = (string)(r.Element("Birthdate")) == string.Empty ? DateTimeParser((DateTime)(r.Element("Birthdate"))) : DateTime.Now,
    Email = (string)r.Element("Email") != null ? (string)r.Element("Email") : "",
    FamilyStatus = (string)r.Element("Familystatus") != null ? (string)r.Element("Familystatus") : "",
    ID = (int)r.Element("IdNumber") != 0 ? (int)r.Element("IdNumber") : 0,
    Phone1 = (int)r.Element("Telephone1") != 0 ? (int)r.Element("Telephone1") : 0,
    Phone2 = (int)r.Element("Telephone2") != 0 ? (int)r.Element("Telephone2") : 0,
    phone3 = (int)r.Element("Telephone3") != 0 ? (int)r.Element("Telephone3") : 0
  };
  return member.ToList();
}

The xml

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
  <UpdateMember xmlns="http://tempuri.org/">
    <member>
      <Birthdate>25122012</Birthdate>
      <Email>fake@testing.com</Email>
      <Familystatus>single</Familystatus>
      <IdNumber>12345678</IdNumber>
      <Telephone1>123-4567890</Telephone1>
      <Telephone2></Telephone2>
      <Telephone3></Telephone3>
    </member>
  </UpdateMember>

In the line from r in doc.Decendants("")...
I have tried all kind of variations.
Thank to all

  • 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-15T22:37:48+00:00Added an answer on June 15, 2026 at 10:37 pm

    The problem is namespaces:

    <UpdateMember xmlns="http://tempuri.org/">
    

    That means all your elements are in that namespace, by default. Your queries all try to look for elements which aren’t in a namespace, e.g.

    Element("Familystatus")
    

    This is very easy to fix in LINQ to XML:

    XNamespace ns = "http://tempuri.org/";
    IEnumerable<UpdateMember> member = from r in doc.Descendants(ns + "UpdateMember")
                                                    .Descendants(ns + "member")
    // etc
    

    Additionally, your attempt to handle missing data won’t work and is overly complicated. For example, this:

    Phone1 = (int)r.Element("Telephone1") != 0 ? (int)r.Element("Telephone1") : 0
    

    should be:

    Phone1 = (int?) r.Element(ns + "Telephone1") ?? 0
    

    If you cast to int, it won’t give a value of 0 if the value is missing – it will throw an exception. Casting to int? will give a null value instead.

    I would change your whole code to:

    return doc.Descendants(ns + "UpdateMember").Descendants(ns + "member")
              .Select (r => new UpdateMember
              {
                  // Really? This is an odd default.
                  Birthdate = (DateTime?) r.Element(ns + "Birthdate") ?? DateTime.Now,
                  Email = (string) r.Element(ns + "Email") ?? "",
                  FamilyStatus = (string) r.Element(ns + "Familystatus) ?? "",
                  ID = (int?) r.Element(ns + "IDNumber") ?? 0,
                  Phone1 = (int?) r.Element(ns + "Telephone1") ?? 0,
                  Phone2 = (int?) r.Element(ns + "Telephone2") ?? 0,
                  Phone3 = (int?) r.Element(ns + "Telephone3") ?? 0)
              })
              .ToList();
    

    EDIT: As noted in comments, storing phone numbers as int values is a really bad idea. You’d lose any formatting, you’d lose leading zeroes, and I’d expect many full phone numbers to be outside the range of an int anyway. I’d keep them as strings.

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

Sidebar

Related Questions

I have a class that various different XML schemes are created from. I create
I'm trying to extract some data from XML using Linq to XML, and I
Using LINQ to Entities, how can I determine if any item from a List
i have a list of event Ids returned from an xml document as shown
Working with Sitecore and Linq extensions. I am trying to convert to from an
The task is to convert the CSV file into XML. var x = from
I have this XML file that I parse into its elements and create a
Trying to retrieve data using linq from a database. I would like to use
I am trying to get a single row from a data table using LINQ.
I am trying to return a binary from the DB using linq for display

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.