2012-04-25 59 views
0

My Earlier post加入XML元素和處理如果不退出 - 使用LINQ到XML ASP.Net C#

延伸到我自己的問題如果你發現我與加入我的元素讓關鍵字

有時候我的XML可能沒有全部這些元素。

所以如果退出,就可以加入這個元素了嗎? 如果不存在給予空或空值與用於該領域

例如,如果沒有藥房元。

那麼這let Pharmacy = MD.Element(NameSpace + "Pharmacy") 應該是可能的,這

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

應該給我一個空或空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(); 

回答

0

終於找到了解決辦法來對付它參照這個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? 
0

而不是做這個的:

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

使用顯式轉換操作是這樣的:

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

它將返回.value的,如果該元素不爲空,或者如果元素不存在一個空字符串。在這裏,你可以找到明確的轉換運營商的更多詳細信息的XElement:http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.op_explicit.aspx

+0

以及這可能在選擇電平工作,但我有問題,連接件。我如何加入可能或不可能退出的元素。 – HaBo 2012-04-30 14:33:32