0

Extending to my own question from My Earlier post

If you notice i am joining my elements with let keyword

some times my XML may not have all these elements.

So is it possible to join this element on if exits? if not exists give a null or empty value for that field

for example if there is no Pharmacy Element.

then this let Pharmacy = MD.Element(NameSpace + "Pharmacy") should be possible and this

phStoreName = Pharmacy.Element(NameSpace + "StoreName").Value,

should give me a null or empty value for phStoreName

var MedicationDispensed =
  (from MD in xdoc.Descendants(NameSpace + "MedicationDispensed")
   let DrugCoded = MD.Element(NameSpace + "DrugCoded")
   let Quantity = MD.Element(NameSpace + "Quantity")
   let Pharmacy = MD.Element(NameSpace + "Pharmacy")
   let phIdentification = Pharmacy.Element(NameSpace + "Identification")
   let phAddress = Pharmacy.Element(NameSpace + "Address")
   let phPhoneNumbers = Pharmacy.Element(NameSpace + "PhoneNumbers")
   let phPhone = phPhoneNumbers.Element(NameSpace + "Phone")
   let Prescriber = MD.Element(NameSpace + "Prescriber")
   let prIdentification = Prescriber.Element(NameSpace + "Identification")
   let prName = Prescriber.Element(NameSpace + "Name")
   let prAddress = Prescriber.Element(NameSpace + "Address")
   select new
   {
       DrugDescription = MD.Element(NameSpace + "DrugDescription").Value,
       ProductCode = DrugCoded.Element(NameSpace + "ProductCode").Value,
       ProductCodeQualifier = DrugCoded.Element(NameSpace + "ProductCodeQualifier").Value,
       Qualifier = Quantity.Element(NameSpace + "Qualifier").Value,
       Value = Quantity.Element(NameSpace + "Value").Value,
       CodeListQualifier = Quantity.Element(NameSpace + "CodeListQualifier").Value,
       DaysSupply = MD.Element(NameSpace + "DaysSupply").Value,
       LastFillDate = MD.Element(NameSpace + "LastFillDate").Value,
       phStoreName = Pharmacy.Element(NameSpace + "StoreName").Value,
       phNCPDPID = phIdentification.Element(NameSpace + "NCPDPID").Value,
       phAddress1 = phAddress.Element(NameSpace + "AddressLine1").Value,
       phCity = phAddress.Element(NameSpace + "City").Value,
       phState = phAddress.Element(NameSpace + "State").Value,
       phZipcode = phAddress.Element(NameSpace + "ZipCode").Value,
       phPhoneNumber = phPhone.Element(NameSpace + "Number").Value,
       phQualifier = phPhone.Element(NameSpace + "Qualifier").Value,
       prDEANumber = prIdentification.Element(NameSpace + "DEANumber").Value,
       prLastName = prName.Element(NameSpace + "LastName").Value,
       prFirstName = prName.Element(NameSpace + "FirstName").Value,
       prMiddleName = prName.Element(NameSpace + "MiddleName").Value,
       prAddress1 = prAddress.Element(NameSpace + "AddressLine1").Value,
       prCity = prAddress.Element(NameSpace + "City").Value,
       prState = prAddress.Element(NameSpace + "State").Value,
       prZipCode = prAddress.Element(NameSpace + "ZipCode").Value
   }).ToList();

2 Answers 2

0

Instead of doing this:

Pharmacy.Element(NameSpace + "StoreName").Value

use explicit conversion operator like this:

(string)Pharmacy.Element(NameSpace + "StoreName")

It will return .Value if the element is not null or an empty string if the element does not exist. Here you can find more details about explicit conversion operators for XElement: http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.op_explicit.aspx

1
  • well this may work at select level but i have problem with joining elements. how can i join the elements which may or may not exits.
    – HaBo
    Apr 30, 2012 at 14:33
0

Finally found a solution to deal with it Referring to this Link

    What if "let" returns null?

If you declare an object with "let" such as

let myPayment = bill.Element("payment")

select new Payment

  {

     Amount = (decimal) myPayment.Element("Amount")

  }

If myPayment is null, this blows up with a null reference exception.  The alternative would be

  Amount = myPayment != null ? (decimal) myPayment.Element("Amount")

but think how cumbersome that is for a bunch of fields.

What's the solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.