Solved

Embedd an if statement into xml Tree building...

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

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 74

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 74

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now