2014-09-29 22 views
0

我有下面的xml字符串,我試圖根據動態的一組列來查詢數據表。我對xml沒有任何控制權,它正從外部源傳入。動態地將XML讀取到Datatable

XML:

<VmapMessage> 
    <MessageBody> 
    <ClaimId>346117125.01*BR</ClaimId> 
    <LastName>SMITH</LastName> 
    <FirstName>JOHN</FirstName> 
    <ClaimNumber>20222128369154</ClaimNumber> 
    <DOI>20131006</DOI> 
    <SOJ>OH</SOJ> 
    <EmailType>V2</EmailType> 
    <AdjusterName>STEPHAN WILLIAMS</AdjusterName> 
    <AdjusterId>TIWI</AdjusterId> 
    <AdjusterEmail>[email protected]</AdjusterEmail> 
    <SendAdjusterEmailAsHTML>T</SendAdjusterEmailAsHTML> 
    <AdjusterTimeZone>-8</AdjusterTimeZone> 
    <AdjusterDelegateName></AdjusterDelegateName> 
    <AdjusterDelegateId></AdjusterDelegateId> 
    <AdjusterDelegateEmail></AdjusterDelegateEmail> 
    <SendAdjusterDelegateEmailAsHTML></SendAdjusterDelegateEmailAsHTML> 
    <AdjusterDelegateTimeZone></AdjusterDelegateTimeZone> 
    <AdjusterDelegateStartDate></AdjusterDelegateStartDate> 
    <AdjusterDelegateEndDate></AdjusterDelegateEndDate> 
    <SupervisorEmail>[email protected]</SupervisorEmail> 
    <SendSupervisorEmailAsHTML>T</SendSupervisorEmailAsHTML> 
    <CarrierEmployee>N</CarrierEmployee> 
    <EncryptedSubId>232228520C6IB</EncryptedSubId> 
    <ChampionEmail>[email protected]</ChampionEmail> 
    <MasterCarrierInfo> 
     <MasterCarrierCode>COMP</MasterCarrierCode> 
     <CarrierName>COMP'S</CarrierName> 
     <VitalPointURL></VitalPointURL> 
     <EmployerCode>00000016</EmployerCode> 
     <OfficeCode>COMPREDCA</OfficeCode> 
    </MasterCarrierInfo> 
    <Details> 
     <Detail> 
     <Id>0005111*17228*DR3211</Id> 
     <ClaimId></ClaimId> 
     <RxNumber>0005073</RxNumber> 
     <RxDate>20140725</RxDate> 
     <DateEntered>20140917</DateEntered> 
     <FillNumber></FillNumber> 
     <ServiceType></ServiceType> 
     <NDC>72344299106</NDC> 
     <GPI>75100</GPI> 
     <DrugClass>*SKELETAL MUSCLE RELAXANTS*</DrugClass> 
     <ServiceDescription>CYCLOBENZAPR CRE 20MG/GM</ServiceDescription> 
     <Quantity>60</Quantity> 
     <UnitOfMeasure></UnitOfMeasure> 
     <NumberOfRefills>0</NumberOfRefills> 
     <DaysSupply>30</DaysSupply> 
     <BillToCarrierAmount>0</BillToCarrierAmount> 
     <InvoiceAmount>188.77</InvoiceAmount> 
     <PrescriberNumber></PrescriberNumber> 
     <PrescriberName>SMITH, JOHN F MD</PrescriberName> 
     <PrescriberPhone></PrescriberPhone> 
     <DAW>0</DAW> 
     <BrandGeneric>MULTISOURCE</BrandGeneric> 
     <ICD9></ICD9> 
     <DiagnosisDesc></DiagnosisDesc> 
     <HCPC></HCPC> 
     <RejectCode>75S75</RejectCode> 
     <RejectReason>FM - Missing Pharmacy Telephone NumberüNDC NOT COVERED</RejectReason> 
     <PharmacyName>JOHN SMITH MD</PharmacyName> 
     <PharmacyPhysAddress1>6222 N SECOND ST</PharmacyPhysAddress1> 
     <PharmacyPhysAddress2></PharmacyPhysAddress2> 
     <PharmacyPhysCity>FRESNO</PharmacyPhysCity> 
     <PharmacyPhysState>CA</PharmacyPhysState> 
     <PharmacyPhysZip>937105446</PharmacyPhysZip> 
     <PharmacyPhone>9493342222</PharmacyPhone> 
     <StSub>0</StSub> 
     <Reg>0</Reg> 
     <MedStat>Y</MedStat> 
     <RegExcp>0</RegExcp> 
     <RejectCodes></RejectCodes> 
     <EmployerName>COMP'S WEST STORES, INC.</EmployerName> 
     <FirstBlock></FirstBlock> 
     <Blocks> 
      <Block> 
      <BlockCode>75*ndc</BlockCode> 
      <BlockReason>NDC NOT COVERED</BlockReason> 
      <Instance>1</Instance> 
      </Block> 
      <Block> 
      <BlockCode>76*ndc</BlockCode> 
      <BlockReason>NDC NOT KNOWN</BlockReason> 
      <Instance>2</Instance> 
      </Block> 
     </Blocks> 
     </Detail> 
     <Detail> 
     <Id>0005111*17228*DR3212</Id> 
     <ClaimId></ClaimId> 
     <RxNumber>0005074</RxNumber> 
     <RxDate>20140725</RxDate> 
     <DateEntered>20140917</DateEntered> 
     <FillNumber></FillNumber> 
     <ServiceType></ServiceType> 
     <NDC>72344299102</NDC> 
     <GPI>123456789</GPI> 
     <DrugClass>*SKELETAL MUSCLE RELAXANTS*</DrugClass> 
     <ServiceDescription>CYCLOBENZAPR CRE 20MG/GM</ServiceDescription> 
     <Quantity>60</Quantity> 
     <UnitOfMeasure></UnitOfMeasure> 
     <NumberOfRefills>0</NumberOfRefills> 
     <DaysSupply>30</DaysSupply> 
     <BillToCarrierAmount>0</BillToCarrierAmount> 
     <InvoiceAmount>188.77</InvoiceAmount> 
     <PrescriberNumber></PrescriberNumber> 
     <PrescriberName>SMITH, JOHN F MD</PrescriberName> 
     <PrescriberPhone></PrescriberPhone> 
     <DAW>0</DAW> 
     <BrandGeneric>MULTISOURCE</BrandGeneric> 
     <ICD9></ICD9> 
     <DiagnosisDesc></DiagnosisDesc> 
     <HCPC></HCPC> 
     <RejectCode>75S75</RejectCode> 
     <RejectReason>FM - Missing Pharmacy Telephone NumberNDC NOT COVERED</RejectReason> 
     <PharmacyName>JOHN SMITH MD</PharmacyName> 
     <PharmacyPhysAddress1>6222 N SECOND ST</PharmacyPhysAddress1> 
     <PharmacyPhysAddress2></PharmacyPhysAddress2> 
     <PharmacyPhysCity>FRESNO</PharmacyPhysCity> 
     <PharmacyPhysState>CA</PharmacyPhysState> 
     <PharmacyPhysZip>937105446</PharmacyPhysZip> 
     <PharmacyPhone>9493342222</PharmacyPhone> 
     <StSub>0</StSub> 
     <Reg>0</Reg> 
     <MedStat>Y</MedStat> 
     <RegExcp>0</RegExcp> 
     <RejectCodes></RejectCodes> 
     <EmployerName>COMP'S WEST STORES, INC.</EmployerName> 
     <FirstBlock></FirstBlock> 
     <Blocks> 
      <Block> 
      <BlockCode>75*ndc</BlockCode> 
      <BlockReason>NDC NOT COVERED</BlockReason> 
      <Instance>1</Instance> 
      </Block> 
     </Blocks> 
     </Detail> 
    </Details> 
    </MessageBody> 
</VmapMessage> 

動態輸出所需的字段列表(這是正在從數據庫讀取,並且可以更改):

ParameterName ParameterPath 
AdjusterEmail //MasterCarrierInfo//AdjusterEmail 
EmpCode   //MasterCarrierInfo//EmployerCode 
SOJ    //MessageBody//SOJ 
DOI    //MessageBody//DOI 
GPI    //MessageBody//Details//Detail//GPI 
BlockCode  //MessageBody//Details//Detail//Blocks//Block//BlockCode 

我需要讀入一個表,看起來這像這樣:

AdjusterEmail     EmpCode SOJ DOI  GPI    BlockCode 
[email protected] 00000016 OH 20131006 75100*ndc 
[email protected] 00000016 OH 20131006 75100*ndc 
[email protected] 00000016 OH 20131006 123456789*ndc 

每個MessageBody可以有多個詳細記錄,每個D可以有多個Block記錄etail

我真的不知道從哪裏開始,我已經嘗試了一堆不同的東西,包括LINQ(我不知道),並不能算出這個...

+0

嘗試在後續步驟來使用: HTTP:/ /stackoverflow.com/questions/3458566/how-do-i-find-a-xml-node-by-path-in-linq-to-xml – W92 2014-09-29 22:17:34

+0

你可以通過它t他將整個XML blob作爲SQL Server中的XML字段,然後使用XQuery從傳入的XML變量中進行選擇,並使用該語句在表中插入相應的值:http://msdn.microsoft.com/en-us /library/ms189075.aspx – 2014-09-29 22:19:30

+0

@Jonathan Fields你解決了嗎? – 2014-09-30 12:49:30

回答

0

你可以簡單地用以下方法並傳遞XML文件路徑作爲輸入

DataSet ds = new DataSet(); 
ds.ReadXml("Your File Path"); 

你將不得不數據集將包含相應的DataSet對象爲XML。

1

它的工作原理:

XDocument xd = null; 
     using (StreamReader oReader = new StreamReader(xmlFilePath, Encoding.GetEncoding("ISO-8859-1"))) 
     { 
      xd = XDocument.Load(oReader); 
     } 

var records = from root in xd.Descendants("MessageBody") 
         from details in root.Elements("Details").Elements("Detail") 
         select new 
         { 
          AdjusterEmail = root.Element("AdjusterEmail").Value, 
          EmpCode = root.Element("MasterCarrierInfo").Element("EmployerCode").Value, 
          SOJ = root.Element("SOJ").Value, 
          DOI = root.Element("DOI").Value, 
          GPI = details.Element("GPI").Value, 
          BlockCode = details.Element("Blocks").Element("Block").Element("BlockCode").Value 

         }; 

創建數據表來保存記錄:

 DataTable dt = new DataTable(); 
     dt.Columns.Add("AdjusterEmail", typeof(string)); 
     dt.Columns.Add("EmpCode", typeof(string)); 
     dt.Columns.Add("SOJ", typeof(string)); 
     dt.Columns.Add("DOI", typeof(string)); 
     dt.Columns.Add("GPI", typeof(string)); 
     dt.Columns.Add("BlockCode", typeof(string)); 

     DataRow dr = null; 
     foreach (var readValue in records) 
     { 
      dr = dt.NewRow(); 
      dr["AdjusterEmail"] = readValue.AdjusterEmail; 
      dr["EmpCode"] = readValue.EmpCode; 
      dr["SOJ"] = readValue.SOJ; 
      dr["DOI"] = readValue.DOI; 
      dr["GPI"] = readValue.GPI; 
      dr["BlockCode"] = readValue.BlockCode; 
      dt.Rows.Add(dr); 
     } 

DT包含如下:

AdjusterEmail    EmpCode SOJ DOI  GPI    BlockCode 
[email protected] 00000016 OH 20131006 75100*ndc 
[email protected] 00000016 OH 20131006 75100*ndc 
[email protected] 00000016 OH 20131006 123456789*ndc 
+0

這仍然只能帶回一條記錄。每個消息體可以有多個細節,每個細節可以有多個塊。請參閱上面編輯的xml。 – 2014-10-07 18:13:49

+0

此外,字段列表是動態的,我正在從數據庫中讀取它們,因此下次可能會有其他字段或更少的字段。 – 2014-10-07 20:20:05

+0

@JonathanFields現在它讀取所有細節節點 – 2014-10-08 05:01:23