Parse complex xml to fetch particular node text in c#












1














Parsing XML file using SSIS / C#



operations like fetching record count from trailer , TIN from body and store into a variable or somewhere temporarily(your suggestions please) for further processing. I don't want to store it in a table.



Please find the sample xml mentioned below





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











share|improve this question
























  • does your xml have blank namespace like xmlns:xsi="" or may be it contains some value?
    – er-shoaib
    Nov 19 '18 at 13:32










  • There is no name space tag, anyway Does it really matters?
    – Santhana
    Nov 19 '18 at 13:35










  • yes xml parsing is depends upon namespace so can i assume that there is no any namespace?
    – er-shoaib
    Nov 19 '18 at 13:36










  • @er-shoaib yes please
    – Santhana
    Nov 19 '18 at 13:37










  • so you need to fetch all TIN for all Participants in list right?
    – er-shoaib
    Nov 19 '18 at 13:38
















1














Parsing XML file using SSIS / C#



operations like fetching record count from trailer , TIN from body and store into a variable or somewhere temporarily(your suggestions please) for further processing. I don't want to store it in a table.



Please find the sample xml mentioned below





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











share|improve this question
























  • does your xml have blank namespace like xmlns:xsi="" or may be it contains some value?
    – er-shoaib
    Nov 19 '18 at 13:32










  • There is no name space tag, anyway Does it really matters?
    – Santhana
    Nov 19 '18 at 13:35










  • yes xml parsing is depends upon namespace so can i assume that there is no any namespace?
    – er-shoaib
    Nov 19 '18 at 13:36










  • @er-shoaib yes please
    – Santhana
    Nov 19 '18 at 13:37










  • so you need to fetch all TIN for all Participants in list right?
    – er-shoaib
    Nov 19 '18 at 13:38














1












1








1







Parsing XML file using SSIS / C#



operations like fetching record count from trailer , TIN from body and store into a variable or somewhere temporarily(your suggestions please) for further processing. I don't want to store it in a table.



Please find the sample xml mentioned below





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











share|improve this question















Parsing XML file using SSIS / C#



operations like fetching record count from trailer , TIN from body and store into a variable or somewhere temporarily(your suggestions please) for further processing. I don't want to store it in a table.



Please find the sample xml mentioned below





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








c# xml ssis-2017






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 13:59









er-shoaib

4,9492516




4,9492516










asked Nov 19 '18 at 13:10









Santhana

10110




10110












  • does your xml have blank namespace like xmlns:xsi="" or may be it contains some value?
    – er-shoaib
    Nov 19 '18 at 13:32










  • There is no name space tag, anyway Does it really matters?
    – Santhana
    Nov 19 '18 at 13:35










  • yes xml parsing is depends upon namespace so can i assume that there is no any namespace?
    – er-shoaib
    Nov 19 '18 at 13:36










  • @er-shoaib yes please
    – Santhana
    Nov 19 '18 at 13:37










  • so you need to fetch all TIN for all Participants in list right?
    – er-shoaib
    Nov 19 '18 at 13:38


















  • does your xml have blank namespace like xmlns:xsi="" or may be it contains some value?
    – er-shoaib
    Nov 19 '18 at 13:32










  • There is no name space tag, anyway Does it really matters?
    – Santhana
    Nov 19 '18 at 13:35










  • yes xml parsing is depends upon namespace so can i assume that there is no any namespace?
    – er-shoaib
    Nov 19 '18 at 13:36










  • @er-shoaib yes please
    – Santhana
    Nov 19 '18 at 13:37










  • so you need to fetch all TIN for all Participants in list right?
    – er-shoaib
    Nov 19 '18 at 13:38
















does your xml have blank namespace like xmlns:xsi="" or may be it contains some value?
– er-shoaib
Nov 19 '18 at 13:32




does your xml have blank namespace like xmlns:xsi="" or may be it contains some value?
– er-shoaib
Nov 19 '18 at 13:32












There is no name space tag, anyway Does it really matters?
– Santhana
Nov 19 '18 at 13:35




There is no name space tag, anyway Does it really matters?
– Santhana
Nov 19 '18 at 13:35












yes xml parsing is depends upon namespace so can i assume that there is no any namespace?
– er-shoaib
Nov 19 '18 at 13:36




yes xml parsing is depends upon namespace so can i assume that there is no any namespace?
– er-shoaib
Nov 19 '18 at 13:36












@er-shoaib yes please
– Santhana
Nov 19 '18 at 13:37




@er-shoaib yes please
– Santhana
Nov 19 '18 at 13:37












so you need to fetch all TIN for all Participants in list right?
– er-shoaib
Nov 19 '18 at 13:38




so you need to fetch all TIN for all Participants in list right?
– er-shoaib
Nov 19 '18 at 13:38












2 Answers
2






active

oldest

votes


















1














You need to get fist get list of Participants then fetch all Participants tin number into list like



Here i created console app for your demonstration purpose.



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


Output: (For 2 Participants)



enter image description here






share|improve this answer





























    1














    You need to create a class for each node and use XML deserialisation to create the object.



    I had to remove the empty namespace as the deserialisation process requires a valid namespace.



    Also you can change the type of the properties according to your needs.



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


    Output:



    123456789
    123456780





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375381%2fparse-complex-xml-to-fetch-particular-node-text-in-c-sharp%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You need to get fist get list of Participants then fetch all Participants tin number into list like



      Here i created console app for your demonstration purpose.



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


      Output: (For 2 Participants)



      enter image description here






      share|improve this answer


























        1














        You need to get fist get list of Participants then fetch all Participants tin number into list like



        Here i created console app for your demonstration purpose.



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


        Output: (For 2 Participants)



        enter image description here






        share|improve this answer
























          1












          1








          1






          You need to get fist get list of Participants then fetch all Participants tin number into list like



          Here i created console app for your demonstration purpose.



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


          Output: (For 2 Participants)



          enter image description here






          share|improve this answer












          You need to get fist get list of Participants then fetch all Participants tin number into list like



          Here i created console app for your demonstration purpose.



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


          Output: (For 2 Participants)



          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 13:48









          er-shoaib

          4,9492516




          4,9492516

























              1














              You need to create a class for each node and use XML deserialisation to create the object.



              I had to remove the empty namespace as the deserialisation process requires a valid namespace.



              Also you can change the type of the properties according to your needs.



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


              Output:



              123456789
              123456780





              share|improve this answer




























                1














                You need to create a class for each node and use XML deserialisation to create the object.



                I had to remove the empty namespace as the deserialisation process requires a valid namespace.



                Also you can change the type of the properties according to your needs.



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


                Output:



                123456789
                123456780





                share|improve this answer


























                  1












                  1








                  1






                  You need to create a class for each node and use XML deserialisation to create the object.



                  I had to remove the empty namespace as the deserialisation process requires a valid namespace.



                  Also you can change the type of the properties according to your needs.



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


                  Output:



                  123456789
                  123456780





                  share|improve this answer














                  You need to create a class for each node and use XML deserialisation to create the object.



                  I had to remove the empty namespace as the deserialisation process requires a valid namespace.



                  Also you can change the type of the properties according to your needs.



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


                  Output:



                  123456789
                  123456780






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 19 '18 at 13:54

























                  answered Nov 19 '18 at 13:48









                  Sergiu Muresan

                  2996




                  2996






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375381%2fparse-complex-xml-to-fetch-particular-node-text-in-c-sharp%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter