Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Empty elements

Empty elements

Scheduled Pinned Locked Moved LINQ
databasexmlhelpquestion
4 Posts 4 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    carrigart
    wrote on last edited by
    #1

    Hey, So I have a simple person class as follows

    public class Person
    {

        public string Name { get; set; }
        public string Sex { get; set; }
        public int Age { get; set; }
    }
    

    and my xml looks like this

    Colin
    34
    Male
    

    Joe
    23
    Male

    Mary
    Female

    As you can see, one of the elements is empty and I'm trying to account for this in my query. I have tried

    var results = (from p in xdoc.Descendants("Person")
    select new Person
    {
    Name = p.Element("Name").Value,
    Sex = p.Element("Sex").Value,
    Age = int.Parse(p.Element("Age").Value ?? "0")
    }).ToList();

    but I get a FormatException with the message "Input string was not in a correct format". Anyone got any ideas of how I can deal with this. I know that if my Age property was a string, then I can get it working but I want to keep it an int. Any help would be great Thanks

    J L 2 Replies Last reply
    0
    • C carrigart

      Hey, So I have a simple person class as follows

      public class Person
      {

          public string Name { get; set; }
          public string Sex { get; set; }
          public int Age { get; set; }
      }
      

      and my xml looks like this

      Colin
      34
      Male
      

      Joe
      23
      Male

      Mary
      Female

      As you can see, one of the elements is empty and I'm trying to account for this in my query. I have tried

      var results = (from p in xdoc.Descendants("Person")
      select new Person
      {
      Name = p.Element("Name").Value,
      Sex = p.Element("Sex").Value,
      Age = int.Parse(p.Element("Age").Value ?? "0")
      }).ToList();

      but I get a FormatException with the message "Input string was not in a correct format". Anyone got any ideas of how I can deal with this. I know that if my Age property was a string, then I can get it working but I want to keep it an int. Any help would be great Thanks

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Age = int.Parse( String.IsNullOrEmpty(p.Element("Age").Value) ? "0" : p.Element("Age").Value)

      S 1 Reply Last reply
      0
      • C carrigart

        Hey, So I have a simple person class as follows

        public class Person
        {

            public string Name { get; set; }
            public string Sex { get; set; }
            public int Age { get; set; }
        }
        

        and my xml looks like this

        Colin
        34
        Male
        

        Joe
        23
        Male

        Mary
        Female

        As you can see, one of the elements is empty and I'm trying to account for this in my query. I have tried

        var results = (from p in xdoc.Descendants("Person")
        select new Person
        {
        Name = p.Element("Name").Value,
        Sex = p.Element("Sex").Value,
        Age = int.Parse(p.Element("Age").Value ?? "0")
        }).ToList();

        but I get a FormatException with the message "Input string was not in a correct format". Anyone got any ideas of how I can deal with this. I know that if my Age property was a string, then I can get it working but I want to keep it an int. Any help would be great Thanks

        L Offline
        L Offline
        L Viljoen
        wrote on last edited by
        #3

        the ?? statement means that is the value is null it should be changed to 0, a empty string is still a value if you wish to make a nullable property use ? after the type declaration Eg public string? Name { get; set; } public string? Sex { get; set; } public int? Age { get; set; } This will mean that when you referencing the property you have two sub properties of relevance HasValue and Value

        Chona1171 Web Developer (C#), Silverlight

        1 Reply Last reply
        0
        • J J4amieC

          Age = int.Parse( String.IsNullOrEmpty(p.Element("Age").Value) ? "0" : p.Element("Age").Value)

          S Offline
          S Offline
          Sanjay J Patolia
          wrote on last edited by
          #4

          Yes correct. This should work without any format exception. :) Thanks

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups