locked
How to ready xml response result in c# asp.net RRS feed

  • Question

  • User-471420332 posted

    How to get id value from xml response result.

    <response uri="/recruit/private/xml/Candidates/addRecords">
    <result>
    <message>Record(s) already exists</message>
    <recorddetail>
    <FL val="Id">316886000014241044</FL>
    <FL val="Created Time">2018-12-14 20:26:50</FL>
    <FL val="Modified Time">2018-12-14 20:26:50</FL>
    <FL val="Created By"><![CDATA[d Pvt Ltd]]></FL>
    <FL val="Modified By"><![CDATA[d Pvt Ltd]]></FL>
    </recorddetail>
    </result>
    </response>

    I am trying read id value as string but not coming i use below code:

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    string myResponse = "";
    using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
    {
    myResponse = sr.ReadToEnd();
    }
    Response.Write(myResponse);

    var xdoc = XDocument.Parse(myResponse);
    xdoc.Elements("response").Elements("result").Elements("message").Elements("recorddetail").Select(l => new Leads()
    {
    ID = (string)l.Elements("FL").Where(fl => (string)fl.Attribute("val") == "Id").FirstOrDefault(),
     

    });

    public class Leads
    {

    public string ID { get; set; }

    }

    but i am getting output in ID= _Page

    I have prefer this example

    https://stackoverflow.com/questions/14937606/read-attribute-value-pairs-from-xml-file-using-linq/14937772#14937772

    Friday, December 14, 2018 3:16 PM

Answers

  • User-893317190 posted

    Hi mazhar khan india,

    Suppose the response you get is in the format

    <?xml version="1.0" encoding="utf-8" ?>
    <response uri="/recruit/private/xml/Candidates/addRecords">
      <result>
        <message>Record(s) already exists</message>
        <recorddetail>
          <FL val="Id">316886000014241044</FL>
          <FL val="Created Time">2018-12-14 20:26:50</FL>
          <FL val="Modified Time">2018-12-14 20:26:50</FL>
          <FL val="Created By"><![CDATA[d Pvt Ltd]]></FL>
          <FL val="Modified By"><![CDATA[d Pvt Ltd]]></FL>
        </recorddetail>
      </result>
    </response>
    

    You could use linq to xml as follows.

    XDocument doc = XDocument.Load(Server.MapPath("/xml/xmlfile/response.xml"));// I use a xmlfile to replace your response
               Response.Write(doc.Descendants("FL").         //get all the Fl elements
    Where(fl => (string)fl.Attribute("val") == "Id"). // filter the data to get the elemlents whose val is id
    FirstOrDefault().Value //get the first element and print the value of it
    );

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 17, 2018 4:28 AM

All replies

  • User1120430333 posted

    https://www.dotnetcurry.com/linq/564/linq-to-xml-tutorials-examples

    The tutorial will show how to it in using Linq-2-XML.

    Sunday, December 16, 2018 5:24 PM
  • User-893317190 posted

    Hi mazhar khan india,

    Suppose the response you get is in the format

    <?xml version="1.0" encoding="utf-8" ?>
    <response uri="/recruit/private/xml/Candidates/addRecords">
      <result>
        <message>Record(s) already exists</message>
        <recorddetail>
          <FL val="Id">316886000014241044</FL>
          <FL val="Created Time">2018-12-14 20:26:50</FL>
          <FL val="Modified Time">2018-12-14 20:26:50</FL>
          <FL val="Created By"><![CDATA[d Pvt Ltd]]></FL>
          <FL val="Modified By"><![CDATA[d Pvt Ltd]]></FL>
        </recorddetail>
      </result>
    </response>
    

    You could use linq to xml as follows.

    XDocument doc = XDocument.Load(Server.MapPath("/xml/xmlfile/response.xml"));// I use a xmlfile to replace your response
               Response.Write(doc.Descendants("FL").         //get all the Fl elements
    Where(fl => (string)fl.Attribute("val") == "Id"). // filter the data to get the elemlents whose val is id
    FirstOrDefault().Value //get the first element and print the value of it
    );

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 17, 2018 4:28 AM