forum

home / developersection / forums / add xelement only if value exists

Add XElement only if value exists

Pravesh Singh 3257 04-Sep-2013

I'm creating an XDocument using Linq-To-XML, like this:

Order order = GetOrder();

XDocument doc = new XDocument(

    new XElement("purchaseOrder",

        new XElement("Name", order.Name),

        new XElement("Address", order.Address),

        new XElement("Shipper", order.Shipper)

    )

);

So sometimes an order will not have a Shipper, it will be null. In that case, I don't want to include the Shipper element at all.

How can I do that inline in my code when creating the doc?


c# c# 
Updated on 04-Sep-2013

Student


Message
Can you answer this question?

Answer

1 Answers

Liked By