Query Nested XML Elements using Linq-to-XML ASP.Ne

2020-03-31 06:58发布

问题:

in my ASP.Net C# application,

I am trying to read a nested XML Elements to an Anonymous type collections.

here is the XML sample

    <MedicationDispensed xmlns="http://www.ncpdp.org/schema/SCRIPT">
  <DrugDescription>OXYCODONE W/APAP 5/325 TAB</DrugDescription>
  <DrugCoded>
    <ProductCode>00406051205</ProductCode>
    <ProductCodeQualifier>ND</ProductCodeQualifier>
  </DrugCoded>
  <Quantity>
    <Qualifier>00</Qualifier>
    <Value>60.0</Value>
    <CodeListQualifier>87</CodeListQualifier>
  </Quantity>
  <DaysSupply>15</DaysSupply>
  <LastFillDate>2012-04-03</LastFillDate>
  <Pharmacy>
    <Identification>
      <NCPDPID>1234567</NCPDPID>
    </Identification>
    <StoreName>WALGREENS #00000</StoreName>
    <Address>
      <AddressLine1>1 CENTRAL STREET</AddressLine1>
      <City>INDIANAPOLIS</City>
      <State>IN</State>
      <ZipCode>46201</ZipCode>
    </Address>
    <PhoneNumbers>
      <Phone>
        <Number>8005551212</Number>
        <Qualifier>TE</Qualifier>
      </Phone>
    </PhoneNumbers>
  </Pharmacy>
  <Prescriber>
    <Identification>
      <DEANumber>KR4184999</DEANumber>
    </Identification>
    <Name>
      <LastName>SMITH</LastName>
      <FirstName>JOHN</FirstName>
      <MiddleName>E</MiddleName>
    </Name>
    <Address>
      <AddressLine1>MERCY CLINIC</AddressLine1>
      <City>ST. PAUL</City>
      <State>MN</State>
      <ZipCode>55101</ZipCode>
    </Address>
  </Prescriber>
</MedicationDispensed>

I am successful till here

 var MedicationDispensed = (from elem in xdoc.Descendants(NameSpace + "MedicationDispensed")
                                           .Descendants(NameSpace + "DrugCoded")
                                           //.Descendants(NameSpace + "Quantity")
                                       select new
                                       {
                                           DrugDescription = elem.Parent.Element(NameSpace + "DrugDescription").Value,
                                           ProductCode = elem.Element(NameSpace + "ProductCode").Value,
                                           ProductCodeQualifier = elem.Element(NameSpace + "ProductCodeQualifier").Value,
                                           //Qualifier = elem.Parent.Element(NameSpace + "Qualifier").Value,
                                           //Value = elem.Element(NameSpace + "Value").Value,
                                           //CodeListQualifier = elem.Element(NameSpace + "CodeListQualifier").Value,
                                           DaysSupply = elem.Parent.Element(NameSpace + "DaysSupply").Value,
                                           LastFillDate = elem.Parent.Element(NameSpace + "LastFillDate").Value
                                       }).ToList();

I am not able to query for Quantity and further i have to do for Pharmacy and Prescriber. Any help would greatly be appreciated.

回答1:

Well I got my answer with the help of another post in StackOverflow

Here is my code to achieve what i want.

 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();

Hope this will be useful for someone who is in need of same work.