Empty elements
-
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
MaleMary
FemaleAs 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
-
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
MaleMary
FemaleAs 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
-
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
MaleMary
FemaleAs 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
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
-
Age = int.Parse( String.IsNullOrEmpty(p.Element("Age").Value) ? "0" : p.Element("Age").Value)
Yes correct. This should work without any format exception. :) Thanks