【问题标题】:Query Nested XML Elements using Linq-to-XML ASP.Net C#使用 Linq-to-XML ASP.Net C# 查询嵌套的 XML 元素
【发布时间】:2012-04-03 14:46:02
【问题描述】:

在我的 ASP.Net C# 应用程序中,

我正在尝试将嵌套的 XML 元素读取到匿名类型集合中。

这里是 XML 示例

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

到这里我都成功了

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

我无法查询数量,而且我还必须查询 Pharmacy 和 Prescriber。 任何帮助将不胜感激。

【问题讨论】:

    标签: xml asp.net-mvc-3 c#-4.0 linq-to-xml


    【解决方案1】:

    我在StackOverflow 的另一篇帖子的帮助下得到了答案

    这是我实现我想要的代码。

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

    希望这对需要相同工作的人有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多