动态地将XML读取到Datatable

时间:2014-09-29 22:06:47

标签: c# xml linq

我有以下xml字符串,我试图根据动态列集查询数据表。我没有对xml的任何控制,它是从外部源传入的。

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>STEPHAN.WILLIAMS@COMP.COM</AdjusterEmail>
    <SendAdjusterEmailAsHTML>T</SendAdjusterEmailAsHTML>
    <AdjusterTimeZone>-8</AdjusterTimeZone>
    <AdjusterDelegateName></AdjusterDelegateName>
    <AdjusterDelegateId></AdjusterDelegateId>
    <AdjusterDelegateEmail></AdjusterDelegateEmail>
    <SendAdjusterDelegateEmailAsHTML></SendAdjusterDelegateEmailAsHTML>
    <AdjusterDelegateTimeZone></AdjusterDelegateTimeZone>
    <AdjusterDelegateStartDate></AdjusterDelegateStartDate>
    <AdjusterDelegateEndDate></AdjusterDelegateEndDate>
    <SupervisorEmail>BOB.SMITH@COMP.COM</SupervisorEmail>
    <SendSupervisorEmailAsHTML>T</SendSupervisorEmailAsHTML>
    <CarrierEmployee>N</CarrierEmployee>
    <EncryptedSubId>232228520C6IB</EncryptedSubId>
    <ChampionEmail>AMY.SMITH@COMP.COM</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>75100012333720</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>12345678901234</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>

输出中所需的动态字段列表(这是从DB读入并且可以更改):

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
STEPHAN.WILLIAMS@example.COM  00000016  OH   20131006  75100012333720  75*ndc
STEPHAN.WILLIAMS@example.COM  00000016  OH   20131006  75100012333720  76*ndc
STEPHAN.WILLIAMS@example.COM  00000016  OH   20131006  12345678901234  75*ndc

每个MessageBody可以有多个详细记录,每个详细信息可以有多个Block记录

我真的不知道从哪里开始,我已经尝试了很多不同的东西,包括linq(我不知道)并且无法解决这个问题......

2 个答案:

答案 0 :(得分: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
STEPHAN.WILLIAMS@COMP.COM  00000016  OH   20131006  75100012333720  75*ndc
STEPHAN.WILLIAMS@example.COM  00000016  OH   20131006  75100012333720  76*ndc
STEPHAN.WILLIAMS@example.COM  00000016  OH   20131006  12345678901234  75*ndc

答案 1 :(得分:0)

您可以使用下面的方法并将您的xml文件路径作为输入传递

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

您将拥有数据集,其中包含xml的相应数据集对象。