Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Embedd an if statement into xml Tree building...

Posted on 2011-09-11
4
Medium Priority
?
442 Views
Last Modified: 2013-11-11
I am building an xml tree using xElement.  This is my first time using it.  I have attached a code snippit.  It's not the entire snippit, but the end part.  

objThirdParty can be null.  I need to check if it is null, and if it is, i need to put "" (blank) in that field.  I still must include these nodes, I just need to pass it in blank.  I tried to embedd and if statement inside of the object Content argument of the New xElement and I had some errors saying it was invalid.

Any help would be great.  I basically want to do something like if objEntityShipment.objThirdParty != null then objEntityShipment.objThirdParty.COmpany else "".

If possible, i would like to be able to do this wihout writting that code twice....  One with the object, one without the object.
...
new XElement("shipto",
                    new XElement("name", objEntityShipment.objShipTo.strContactname),
                    new XElement("company", objEntityShipment.objShipTo.strCompanyName),
                    new XElement("addr1", objEntityShipment.objShipTo.strAddressLine1),
                    new XElement("addr2", objEntityShipment.objShipTo.strAddressLine2),
                    new XElement("addr3", objEntityShipment.objShipTo.strAddressLine3),
                    new XElement("city", objEntityShipment.objShipTo.strCity),
                    new XElement("state", objEntityShipment.objShipTo.strState),
                    new XElement("zipcode", objEntityShipment.objShipTo.strPostalCode),
                    new XElement("country", objEntityShipment.objShipTo.strCountryCode),
                    new XElement("phone", objEntityShipment.objShipTo.strPhoneNo)
                    ),
                new XElement("billto",
                    new XElement("name", objEntityShipment.objThirdParty.strContactname),
                    new XElement("company", objEntityShipment.objThirdParty.strCompanyName),
                    new XElement("addr1", objEntityShipment.objThirdParty.strAddressLine1),
                    new XElement("addr2", objEntityShipment.objThirdParty.strAddressLine2),
                    new XElement("addr3", objEntityShipment.objThirdParty.strAddressLine3),
                    new XElement("city", objEntityShipment.objThirdParty.strCity),
                    new XElement("state", objEntityShipment.objThirdParty.strState),
                    new XElement("zipcode", objEntityShipment.objThirdParty.strPostalCode),
                    new XElement("country", objEntityShipment.objThirdParty.strCountryCode),
                    new XElement("phone", objEntityShipment.objThirdParty.strPhoneNo),
                    new XElement("billnumber", objEntityShipment.objThirdParty.strAccountNumber)
                    )
            );

Open in new window

0
Comment
Question by:Bran-Damage
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 36519958
I haven't tested this, but see if this works:
new XElement("shipto",
                    new XElement("name", objEntityShipment.objShipTo.strContactname),
                    new XElement("company", objEntityShipment.objShipTo.strCompanyName),
                    new XElement("addr1", objEntityShipment.objShipTo.strAddressLine1),
                    new XElement("addr2", objEntityShipment.objShipTo.strAddressLine2),
                    new XElement("addr3", objEntityShipment.objShipTo.strAddressLine3),
                    new XElement("city", objEntityShipment.objShipTo.strCity),
                    new XElement("state", objEntityShipment.objShipTo.strState),
                    new XElement("zipcode", objEntityShipment.objShipTo.strPostalCode),
                    new XElement("country", objEntityShipment.objShipTo.strCountryCode),
                    new XElement("phone", objEntityShipment.objShipTo.strPhoneNo)
                    ),
                    objEntityShipment == null ? new XElement("shipto", string.Empty) :
                                                new XElement("billto",
                                                    new XElement("name", objEntityShipment.objThirdParty.strContactname),
                                                    new XElement("company", objEntityShipment.objThirdParty.strCompanyName),
                                                    new XElement("addr1", objEntityShipment.objThirdParty.strAddressLine1),
                                                    new XElement("addr2", objEntityShipment.objThirdParty.strAddressLine2),
                                                    new XElement("addr3", objEntityShipment.objThirdParty.strAddressLine3),
                                                    new XElement("city", objEntityShipment.objThirdParty.strCity),
                                                    new XElement("state", objEntityShipment.objThirdParty.strState),
                                                    new XElement("zipcode", objEntityShipment.objThirdParty.strPostalCode),
                                                    new XElement("country", objEntityShipment.objThirdParty.strCountryCode),
                                                    new XElement("phone", objEntityShipment.objThirdParty.strPhoneNo),
                                                    new XElement("billnumber", objEntityShipment.objThirdParty.strAccountNumber)
                                                    )
);

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36519961
Change line 13 to this--I copied the wrong line initially:

objEntityShipment == null ? new XElement("billto", string.Empty) :

Open in new window

0
 

Author Closing Comment

by:Bran-Damage
ID: 36520143
Thanks.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36523256
Sure. Glad to help  = )
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question