XElement = null

  • Thread starter Thread starter Stuart Shay
  • Start date Start date
S

Stuart Shay

Hello All

I have the following code

XElement element = null;

if (trackPointExtensions != null)
element = new XElement(ns + "extensions",
new XElement(gpxtpx + "TrackPointExtension",
new XElement(gpxtpx + "hr",
trackPointExtensions.HeartRate != null ? trackPointExtensions.HeartRate :
null),
new XElement(gpxtpx + "atemp",
trackPointExtensions.DegreesCelsius != null ?
trackPointExtensions.DegreesCelsius : null)
));


Which Produces the Following XML

<extensions>
<gpxtpx:TrackPointExtension>
<gpxtpx:hr>93</gpxtpx:hr>
<gpxtpx:atemp />
</gpxtpx:TrackPointExtension>
</extensions>

When trackPointExtensions.DegreesCelsius = null, I want to create the XML so
<gpxtpx:atemp /> Is not writen, what is the best way this can be done.

Thanks
Stuart
 
Stuart said:
[...]
<extensions>
<gpxtpx:TrackPointExtension>
<gpxtpx:hr>93</gpxtpx:hr>
<gpxtpx:atemp />
</gpxtpx:TrackPointExtension>
</extensions>

When trackPointExtensions.DegreesCelsius = null, I want to create the
XML so <gpxtpx:atemp /> Is not writen, what is the best way this can be
done. [...]

If you pass "null" for one of the elements when creating an element, no
element will be created for that item.

So, you can simply rearrange the check for null a bit, using this code
instead:

if (trackPointExtensions != null)
{
element = new XElement(ns + "extensions",
new XElement(gpxtpx + "TrackPointExtension",
new XElement(gpxtpx + "hr",
trackPointExtensions.HeartRate != null ?
trackPointExtensions.HeartRate : null),
trackPointExtensions.DegreesCelsius != null ?
new XElement(gpxtpx + "atemp",
trackPointExtensions.DegreesCelsius) : null));
}

You can apply the same treatment to the "hr" element if you also don't
want that one present when the value is null.

Pete
 
Pete:

Thanks for the help !!!, I applied the logic to both elements (hr and aTemp)

if (trackPointExtensions != null)
{
element = new XElement(ns + "extensions",
new XElement(gpxtpx + "TrackPointExtension",
trackPointExtensions.HeartRate != null ?
new XElement(gpxtpx + "hr", trackPointExtensions.HeartRate) : null,
trackPointExtensions.DegreesCelsius !=
null ? new XElement(gpxtpx + "atemp",trackPointExtensions.DegreesCelsius) :
null
)

);
}


Best
Stuart




Peter Duniho said:
Stuart said:
[...]
<extensions>
<gpxtpx:TrackPointExtension>
<gpxtpx:hr>93</gpxtpx:hr>
<gpxtpx:atemp />
</gpxtpx:TrackPointExtension>
</extensions>

When trackPointExtensions.DegreesCelsius = null, I want to create the XML
so <gpxtpx:atemp /> Is not writen, what is the best way this can be done.
[...]

If you pass "null" for one of the elements when creating an element, no
element will be created for that item.

So, you can simply rearrange the check for null a bit, using this code
instead:

if (trackPointExtensions != null)
{
element = new XElement(ns + "extensions",
new XElement(gpxtpx + "TrackPointExtension",
new XElement(gpxtpx + "hr",
trackPointExtensions.HeartRate != null ?
trackPointExtensions.HeartRate : null),
trackPointExtensions.DegreesCelsius != null ?
new XElement(gpxtpx + "atemp",
trackPointExtensions.DegreesCelsius) : null));
}

You can apply the same treatment to the "hr" element if you also don't
want that one present when the value is null.

Pete
 
Hi Peter,

I am just wondering and maybe it is just a typing mistake that you copied.

Peter Duniho said:
if (trackPointExtensions != null)
{
element = new XElement(ns + "extensions",
new XElement(gpxtpx + "TrackPointExtension",
new XElement(gpxtpx + "hr",
trackPointExtensions.HeartRate != null ?
trackPointExtensions.HeartRate : null),
trackPointExtensions.DegreesCelsius != null ?
new XElement(gpxtpx + "atemp",
trackPointExtensions.DegreesCelsius) : null));
}

Why this code:
trackPointExtensions.HeartRate != null ? trackPointExtensions.HeartRate :
null
It is simply equal to trackPointExtensions.HeartRate.

I thought, that
trackPointExtensions != null ? trackPointExtensions.HeartRate : null
could be ment (which could make sense to avoid a null reference exception.)

Same with trackPointExtensions.DegreesCelsius check of the author. Your code
with that makes sense now.

But maybe I am missing something / understand something wrong?

With kind regards,

Konrad
 
Konrad said:
[...]
Why this code:
trackPointExtensions.HeartRate != null ? trackPointExtensions.HeartRate
: null
It is simply equal to trackPointExtensions.HeartRate.

I thought, that
trackPointExtensions != null ? trackPointExtensions.HeartRate : null
could be ment (which could make sense to avoid a null reference exception.)

That would be redundant with the if statement containing the whole block
of code.
Same with trackPointExtensions.DegreesCelsius check of the author. Your
code with that makes sense now.

But maybe I am missing something / understand something wrong?

You'd have to ask the OP to know for sure what the intent was.

Sometimes you see something like:

trackPointExtensions.HeartRate != null ?
trackPointExtensions.HeartRate : "";

That sort of thing. But yes, the original code (which I simply copied
and pasted, except for my suggested change) seems to have superfluous
checks for null.

Pete
 
Hi Peter,

thank you for the quick answer. So I didn't miss anything important.

Konrad
 
Back
Top