c# - Add XElement only if value exists -
i'm creating xdocument using linq-to-xml, 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 order not have shipper, null. in case, don't want include shipper element @ all.
how can inline in code when creating doc?
i form xml in parts instead of forming of @ once. (easier read, easier debug)
xdocument doc = new xdocument(); var order = new xelement("purchaseorder", new xelement("name", order.name), new xelement("address", order.address)); if(order.shipper!=null) order.add(new xelement("shipper", order.shipper)); doc.add(order);
Comments
Post a Comment