I am trying to use linq to xml functional construction to create xml from a bunch of objects:
new XAttribute("duration", (m.Media.Type.HasValue && m.Media.Type.Value == MediaType.Image) ? m.DurationInSeconds : default(int?)),
The issue with the code is that if media type does not have a value or media type is not an image, I am getting an exception which probably is because I have used default(int?).
Ideally I would like to replace “null” in the node if media type in not present or media is not image. But could not figure out how.
Any ideas?
Do you want to put the string “null” as the attribute value?
If yes i would use the code below:
This results
<root duration="null" />or<root duration="23" />if typeID == 1