0

I am creating a .net core web api method to get the data from xml file based on request. The data is from soap service. Below is request Parameter. So i am searching based on the request parameter.(producer code)

Request------>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:get="http://xyz/business/profile_management_ebe4x1">
   <soapenv:Header/>
   <soapenv:Body>
      <get:GetProducerInfo>
         <!--Optional:-->
         <GetProducerInfoRequest>
            <ProducerCode>IT9559</ProducerCode>
         </GetProducerInfoRequest>
      </get:GetProducerInfo>
   </soapenv:Body>
</soapenv:Envelope>

Below is the response i am looking Response------->

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <dlwmin:GetProducerInfoResponse xmlns:dlwmin="http://xyz/business/Profile_management_ebe4x1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<GetProducerInfoReply xmlns:ns2="http://xyz/get_access/producer_info" xmlns:ns3="http://yzy/business/profile_reply_ebe4x1">
<Producer>
          <ProducerName>MARSH &amp; ABC company</ProducerName>
          <ResidentState>TEXAS</ResidentState>
          <ResidentCity>MIDLAND</ResidentCity>
          <ProducerStatus>Open</ProducerStatus>
          <ProducerCode>IT6372</ProducerCode>
          <MasterCode>272495</MasterCode>
          <NationalCode>174144</NationalCode>
          <ProducingBranchCode>ABO</ProducingBranchCode>
          <CategoryCode>INT</CategoryCode>
        </Producer>
        <Producer>
          <ProducerName>MARSH &amp; ABC company </ProducerName>
          <ResidentState>MICHIGAN</ResidentState>
          <ResidentCity>LIVONIA</ResidentCity>
          <ProducerStatus>Open</ProducerStatus>
          <ProducerCode>IT9559</ProducerCode>
          <MasterCode>IT9559</MasterCode>
          <NationalCode>174144</NationalCode>
          <LegacyCode>0036604-99999</LegacyCode>
          <ProducingBranchCode>MBO</ProducingBranchCode>
          <CategoryCode>GEN</CategoryCode>
        </Producer>

I have created xml file in .net solution

 XDocument xdoc = XDocument.Load(Path.Combine(Directory.GetCurrentDirectory(), "ProducerResponse.xml"));
            XNamespace ns = "http://xyz/get_access/producer_info";

how i can start read from producer node on request.<producer> always does not not contain equal data. some time it contains 9 or 10. data as it looking from the above data.

TIA

Sam
  • 25
  • 5

1 Answers1

0

Try xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            //put response string here
            string response = File.ReadAllText(FILENAME);

            XDocument doc = XDocument.Parse(response);
            List<Producer> producers = doc.Descendants("Producer").Select(x => new Producer()
            {
                ProducerName = (string)x.Element("ProducerName"),
                ResidentState = (string)x.Element("ResidentState"),
                ResidentCity = (string)x.Element("ResidentCity"),
                ProducerStatus = (string)x.Element("ProducerStatus"),
                ProducerCode = (string)x.Element("ProducerCode"),
                MasterCode = (string)x.Element("MasterCode"),
                NationalCode = (string)x.Element("NationalCode"),
                LegacyCode = (string)x.Element("LegacyCode"),
                ProducingBranchCode = (string)x.Element("ProducingBranchCode"),
                CategoryCode = (string)x.Element("CategoryCode")
            }).ToList();

            Dictionary<string, Producer> dict1 = producers
                .GroupBy(x => x.ProducerCode, y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            Dictionary<string, List<Producer>> dict2 = producers
                .GroupBy(x => x.ProducerCode, y => y)
                .ToDictionary(x => x.Key, y => y.ToList());
        }
    }
    public class Producer
    {
        public string ProducerName { get; set; }
        public string ResidentState  { get; set; }
        public string ResidentCity  { get; set; }
        public string ProducerStatus { get; set; }
        public string ProducerCode { get; set; }
        public string MasterCode { get; set; }
        public string NationalCode { get; set; }
        public string LegacyCode { get; set; }
        public string ProducingBranchCode { get; set; }
        public string CategoryCode { get; set; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Thanks for your response. where we passing the request?. iwant to search based on producer code – Sam Dec 24 '19 at 10:19
  • I just parsed the response. Assumed you would make the request and get the string response from the response. I added two dictionaries to do lookup. – jdweng Dec 24 '19 at 10:24
  • then I have to query the data from dictionary? – Sam Dec 24 '19 at 10:36
  • Yes. I parse the entire xml and put results into classes. So the dictionary does look up from the classes, not the xml. – jdweng Dec 24 '19 at 11:07