1

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>[email protected]</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

6
  • 2
    could you post your XML, too? Commented Dec 6, 2012 at 14:13
  • 1
    So how does doc look like? Commented Dec 6, 2012 at 14:13
  • maybe you should use Member instead of member? Commented Dec 6, 2012 at 14:15
  • Why are you writing x != 0 ? x : 0? Commented Dec 6, 2012 at 14:21
  • 1
    @Danny: Checking for 0 and using 0 instead won't do anything. Commented Dec 6, 2012 at 14:28

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Wouldn't the "-" in the phone number cause a cast exception?
Having problems with the datetime input but i will fix it.Thank you very much
@Cory: If the phone numbers actually include the dashes, yes. Storing phone numbers in an int is definitely not a good idea, but that's somewhat aside from the main issue here. Will note it in an edit though.
Jon Skeet: I have new xml that also has to be in a class. i am having a bit of trouble , can i send the xml?
@Danny: No - ask a new question, if you must, following tinyurl.com/so-list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.