You can use the fact that there's an explicit conversion from XElement
to string
, which returns null for a null XElement
reference. You can then use the null-coalescing operator to go from null to an empty string:
var stds = from std in doc.Descendants("student")
select new
{
ID = std.Element("id").Value,
Name = (string) std.Element("name") ?? "";
};
No comments:
Post a Comment