Solved

Embedd an if statement into xml Tree building...

Posted on 2011-09-11
4
439 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 40
VB.NET - Refactor Class per SOLID principles 2 33
Updating a single record in forEach using C#/Entity 7 25
Powershell Regex Replace Question 5 21
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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