解析复杂 xml 以在 C# 中获取特定节点文本

Parse complex xml to fetch particular node text in c#

正在使用 SSIS/C# 解析 XML 文件

像从 trailer 中获取记录计数、从 body 中获取 TIN 并临时存储到变量或某个地方(请您提出建议)等操作以供进一步处理。我不想将其存储在 table.

请查找下面提到的示例 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>

您需要为每个节点创建一个class并使用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

您需要首先获取 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 名参与者)