Solved

Embedd an if statement into xml Tree building...

Posted on 2011-09-11
4
438 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
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Connection String through a VPN 8 55
Wordpress Cron - Send XML Data to external server FTP 3 22
Where is this file? 3 25
C# Linq Select From List 3 10
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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