Solved

Embedd an if statement into xml Tree building...

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

821 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