0

I am very new to c#. my project to send request in xml format to broker side which i have done it. I am trying to handle the response which again comes in xml format. Below is the format of the response. My question is how to find the tag names that is LocateRequest and LocateStatusApproved and then all those values inside that. I tried below code but I am hardcoding the tag name which i don't want to do that.

XmlNodeList nodeList = xmlDoc.GetElementsByTagName("LocateStatusApproved");          
foreach (XmlNode node in nodeList)
{
    App.logger.LogLine("inside for loop");
    string quant = node.Attributes["approvedQuantity"].Value;
    App.logger.LogLine("quant value: {0}", (quant));
}

Xml response file looks like below

<LocateResponse xmlns="http://www.omnilocate.com/StockLoan/LocateService">
  <Locates>
    <Locate>
      <LocateRequest acceptManualApproval="true" acceptPartial="true" securityId="IBM" securityIdentifierType="TICKER" market="US" requestedQuantity="10" requestorReference="98" cashOrSwap="CASH" />
      <LocateStatusApproved approvedTimeUTC="2020-12-06T18:31:18.214Z" locateId="158705132" approvedQuantity="10" rate="-108.75" rateIndicator="REBATE" />
    </Locate>
  </Locates>
  <Disclaimer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
</LocateResponse>
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Prashant Naik
  • 105
  • 1
  • 8
  • You may get some help from below links : https://stackoverflow.com/questions/13171525/converting-xml-to-a-dynamic-c-sharp-object https://stackoverflow.com/questions/13704752/deserialize-xml-to-object-using-dynamic – Bhavesh Gohel Dec 07 '20 at 07:28

2 Answers2

0

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            StringReader sReader = new StringReader(xml);
            XmlReader xReader = XmlReader.Create(sReader);
            XmlSerializer serializer = new XmlSerializer(typeof(Response));
            Response response = (Response)serializer.Deserialize(xReader);
        }
    }
    [XmlRoot("LocateResponse", Namespace = "http://www.omnilocate.com/StockLoan/LocateService")]
    public class Response
    {
        [XmlArray("Locates", Namespace = "http://www.omnilocate.com/StockLoan/LocateService")]
        [XmlArrayItem("Locate", Namespace = "http://www.omnilocate.com/StockLoan/LocateService")]
        public List<Locate> Locate { get; set; }
    }
    public class Locate
    {
        public LocateRequest LocateRequest { get; set; }
        public LocateStatusApproved LocateStatusApproved { get; set; }
    }
    public class LocateRequest
    {
        [XmlAttribute()]
        public Boolean acceptManualApproval { get; set; }
        [XmlAttribute()]
        public Boolean acceptPartial { get; set; }
        [XmlAttribute()]
        public string securityId { get; set; }
        [XmlAttribute()]
        public string securityIdentifierType { get; set; }
        [XmlAttribute()]
        public string market { get; set; }
        [XmlAttribute()]
        public int requestedQuantity { get; set; }
        [XmlAttribute()]
        public int requestorReference { get; set; }
        [XmlAttribute()]
        public string cashOrSwap { get; set; }
    }
    public class LocateStatusApproved
    {
        [XmlAttribute()]
        public DateTime approvedTimeUTC { get; set; }
        [XmlAttribute()]
        public string locateId { get; set; }
        [XmlAttribute()]
        public int approvedQuantity { get; set; }
        [XmlAttribute()]
        public decimal rate { get; set; }
        [XmlAttribute()]
        public string rateIndicator { get; set; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

Here's how to do it with XPath, and no "hard-coding" class definitions or XPath Names. Hope this is helpful.

XmlDocument xml = new XmlDocument();
xml.Load(HttpContext.Current.Server.MapPath("\\t.xml"));

NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("sl", "http://www.omnilocate.com/StockLoan/LocateService");
StringBuilder sb = new StringBuilder();

foreach( XmlElement ndElement in xml.SelectNodes( "//sl:*", nsmgr)) {
    sb.Append("Element: " + ndElement.Name + "\r\n");
    foreach( XmlAttribute ndAttribute in ndElement.SelectNodes( "@*", nsmgr) ) {
        sb.Append("\tAttribute: " + ndAttribute.Name + "\r\n");
    }
}
Response.ContentType = "text";
Response.Write( sb.ToString() );

Produces this output.

Element: LocateResponse
Element: Locates
Element: Locate
Element: LocateRequest
    Attribute: acceptManualApproval
    Attribute: acceptPartial
    Attribute: securityId
    Attribute: securityIdentifierType
    Attribute: market
    Attribute: requestedQuantity
    Attribute: requestorReference
    Attribute: cashOrSwap
Element: LocateStatusApproved
    Attribute: approvedTimeUTC
    Attribute: locateId
    Attribute: approvedQuantity
    Attribute: rate
    Attribute: rateIndicator
Element: Disclaimer
    Attribute: xsi:nil
William Walseth
  • 2,803
  • 1
  • 23
  • 25