1

使用 SSIS / C# 解析 XML 文件

诸如从预告片中获取记录计数,从正文中获取 TIN 并存储到变量或临时存储到某个地方(请提供您的建议)以进行进一步处理的操作。我不想将它存储在表中。

请找到下面提到的示例 xml

<ACOParticipantData xmlns:xsi="">
  <Header>
    <HeaderCode>HDR_PFPRVDR</HeaderCode>
    <FileCreationDate>20160101</FileCreationDate>
    <ACOProgCode>21</ACOProgCode>
  </Header>
  <Participants>
    <Participant>
      <ACO_ID>V199</ACO_ID>
      <TIN>123456789</TIN>
      <Old_TIN>987654321</Old_TIN>
      <Org_NPI>1234567890</Org_NPI>
      <Ind_NPI>1234567890</Ind_NPI>
      <CCN>123456</CCN>
      <PRG_Eff_Dt>20160101</PRG_Eff_Dt>
      <PRG_Term_Dt>20161231</PRG_Term_Dt>
    </Participant>
  </Participants>
  <Trailer>
    <TrailerCode>TRL_PFPRVDR</TrailerCode>
    <FileCreationDate>20160101</FileCreationDate>
    <RecordCount>1</RecordCount>
  </Trailer>
</ACOParticipantData>
4

2 回答 2

1

您需要为每个节点创建一个类并使用 XML 反序列化来创建对象。

我不得不删除空的命名空间,因为反序列化过程需要一个有效的命名空间。

您也可以根据需要更改属性的类型。

using System;
using System.IO;
using System.Xml.Serialization;
using System.Linq;

public class Program
{
    public class ACOParticipantData 
    {
        public Header Header { get; set; }
        public Participant[] Participants { get; set; }
    }

    public class Header 
    {
        public string HeaderCode { get; set; }
        public string FileCreationDate { get; set; }
        public string ACOProgCode { get; set; }
    }

    public class Participant 
    {
        public string ACO_ID { get; set; }
        public string TIN { get; set; }
        public string Old_TIN { get; set; }
        public string Org_NPI { get; set; }
        public string Ind_NPI { get; set; }
        public string CCN { get; set; }
        public string PRG_Eff_Dt { get; set; }
        public string PRG_Term_Dt { get; set; }
    }

    public class Trailer 
    {
        public string TrailerCode { get; set; }
        public string FileCreationDate { get; set; }
        public string RecordCount { get; set; }
    }

    public static void Main()
    {
        var xmlString = @"<ACOParticipantData>
          <Header>
            <HeaderCode>HDR_PFPRVDR</HeaderCode>
            <FileCreationDate>20160101</FileCreationDate>
            <ACOProgCode>21</ACOProgCode>
          </Header>
          <Participants>
            <Participant>
              <ACO_ID>V199</ACO_ID>
              <TIN>123456789</TIN>
              <Old_TIN>987654321</Old_TIN>
              <Org_NPI>1234567890</Org_NPI>
              <Ind_NPI>1234567890</Ind_NPI>
              <CCN>123456</CCN>
              <PRG_Eff_Dt>20160101</PRG_Eff_Dt>
              <PRG_Term_Dt>20161231</PRG_Term_Dt>
            </Participant>
            <Participant>
              <ACO_ID>V199</ACO_ID>
              <TIN>123456780</TIN>
              <Old_TIN>987654321</Old_TIN>
              <Org_NPI>1234567890</Org_NPI>
              <Ind_NPI>1234567890</Ind_NPI>
              <CCN>123456</CCN>
              <PRG_Eff_Dt>20160101</PRG_Eff_Dt>
              <PRG_Term_Dt>20161231</PRG_Term_Dt>
            </Participant>
          </Participants>
          <Trailer>
            <TrailerCode>TRL_PFPRVDR</TrailerCode>
            <FileCreationDate>20160101</FileCreationDate>
            <RecordCount>1</RecordCount>
          </Trailer>
        </ACOParticipantData>";

        var serializer = new XmlSerializer(typeof(ACOParticipantData));

        ACOParticipantData obj = null;
        using (var reader = new StringReader(xmlString))
        {
            obj = (ACOParticipantData)serializer.Deserialize(reader);
        }

        if (obj == null) 
        {
            return;
        }

        foreach (var tin in obj.Participants.Select(x => x.TIN)) 
        {
            Console.WriteLine(tin);
        }
    }
}

输出:

123456789
123456780
于 2018-11-19T13:48:00.430 回答
1

您需要先获取列表,Participants然后将所有参与者的锡号提取到列表中,例如

在这里,我为您的演示目的创建了控制台应用程序。

class Program
{
    static void Main(string[] args)
    {
        XDocument doc = XDocument.Load(@"Path to your xml file");

        List<long> tinList = new List<long>();

        tinList = doc.Descendants("Participants").Elements().Elements("TIN").Select(x => (long)x).ToList();

        foreach (long tin in tinList)
        {
            Console.WriteLine(tin);
        }

        Console.ReadLine();
    }
}

输出:(2名参与者)

在此处输入图像描述

于 2018-11-19T13:48:15.757 回答