2012-04-03 4 views
0

in meiner ASP.Net C# -Anwendung,Abfrage Nested-XML-Elemente mit Linq-to-XML ASP.Net C#

Ich versuche, ein verschachteltes XML-Elemente zu einer anonymen Typ Sammlungen zu lesen.

Hier ist das XML-Beispiel

<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> 

ich erfolgreich bin bis hier

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

ich nicht in der Lage bin für Menge abzufragen, und ich habe weiter für Pharmazie und Prescriber zu tun. Jede Hilfe würde sehr geschätzt werden.

Antwort

1

Nun habe ich meine Antwort mit Hilfe von einem anderen Beitrag in StackOverflow

Hier ist mein Code zu erreichen, was ich will.

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

Ich hoffe, dies wird für jemanden nützlich sein, der die gleiche Arbeit benötigt.