从我的早期帖子扩展到我自己的问题
如果您注意到我正在使用let关键字加入我的元素
有时我的 XML 可能没有所有这些元素。
那么是否可以在退出时加入此元素?如果不存在,则为该字段提供null 或空值
例如,如果没有Pharmacy Element。
那么这 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();