0

I am trying to send response as per below to the user. I am facing issue for node ITEM in which ( Type, For, Amount, Description & Rate ) present in the node as per below. But when i am trying to generate the output it is not generation output as per below for this node.

EXPECTED OUTPUT

<ANF>
    <QUOTEID>951C3532</QUOTEID>
    <CHARGE>161.98</CHARGE>
    <ITEMIZEDCHARGES>
        <ITEM TYPE="CHARGE" FOR="FREIGHT" AMOUNT="914.41" DESCRIPTION="65 LB CL125, 1 PLT @ 94 x 48 x 12 IN"  />
        <ITEM TYPE="DISCOUNT" FOR="SHIPPER" AMOUNT="-832.11" DESCRIPTION="SHIPPER DISCOUNT" RATE="91%"  />
        <ITEM TYPE="CHARGE" FOR="MCADJ" AMOUNT="137.70" DESCRIPTION="ABSOLUTE MIN CHARGE ADJUSTMENT"  />
    </ITEMIZEDCHARGES> 
    <NOTES>
    </NOTES> 
    <NUMERRORS>0</NUMERRORS>
</ANF>

I have generated the object for this xml response as per below

namespace ResponseXML
{
    [XmlRoot(ElementName="ITEM")]
    public class ITEM {
        [XmlAttribute(AttributeName="TYPE")]
        public string TYPE { get; set; }
        [XmlAttribute(AttributeName="FOR")]
        public string FOR { get; set; }
        [XmlAttribute(AttributeName="AMOUNT")]
        public string AMOUNT { get; set; }
        [XmlAttribute(AttributeName="DESCRIPTION")]
        public string DESCRIPTION { get; set; }
        [XmlAttribute(AttributeName="RATE")]
        public string RATE { get; set; }
    }

    [XmlRoot(ElementName="ITEMIZEDCHARGES")]
    public class ITEMIZEDCHARGES {
        [XmlElement(ElementName="ITEM")]
        public List<ITEM> ITEM { get; set; }
    }

    [XmlRoot(ElementName="ANF")]
    public class ANF {
        [XmlElement(ElementName="QUOTEID")]
        public string QUOTEID { get; set; }
        [XmlElement(ElementName="CHARGE")]
        public string CHARGE { get; set; }
        [XmlElement(ElementName="ITEMIZEDCHARGES")]
        public ITEMIZEDCHARGES ITEMIZEDCHARGES { get; set; }
        [XmlElement(ElementName="NOTES")]
        public string NOTES { get; set; }
        [XmlElement(ElementName="NUMERRORS")]
        public string NUMERRORS { get; set; }
    }

}

My code output ITEMIZEDCHARGES

<ITEMIZEDCHARGES>
        <ITEM>
            <ITEM>
                <AMOUNT>45.09</AMOUNT>
                <DESCRIPTION></DESCRIPTION>
                <FOR i:nil="true" />
                <RATE></RATE>
                <TYPE></TYPE>
            </ITEM>
        </ITEM>
    </ITEMIZEDCHARGES>

My Code for adding values is as below in object

ITEMIZEDCHARGES  _fItem = new  ITEMIZEDCHARGES();
                                List<ITEM> fitmes = new List<ITEM>();
                                ITEM _item = new ITEM();
                                    _item.AMOUNT = Convert.ToString(zipDetails.Rate);
                                    _item.RATE = "";
                                    _item.TYPE = "";
                                    _item.DESCRIPTION = "";
                                fitmes.Add(_item);

                                _fItem.ITEM = fitmes;
                                fn.ITEMIZEDCHARGES = _fItem;

Can you please advice what i am doing mistake.

Code for response from code

[System.Web.Http.HttpPost]
[System.Web.Http.Route("GetQuote")]
[ResponseType(typeof(IEnumerable<ABF>))]
public HttpResponseMessage GetOrderPriceQuotetest(HttpRequestMessage request)
{               
var abf = request.Content.ReadAsStringAsync().Result;       
    try
     {
      if (abf != null)
        {
    XmlSerializer serializer = new XmlSerializer(typeof(OrderPriceRequest.ABF));
    using (StringReader reader = new StringReader(abf))
        {
     OrderPriceRequest.ABF testReq = (OrderPriceRequest.ABF)serializer.Deserialize(reader);  
     if (testReq != null)
          {
           ANF  fn = new ANF();
           fn.CHARGE = 0;
           fn.QUOTEID = "";     
           ITEMIZEDCHARGES _fItem = new ITEMIZEDCHARGES();
            List<ITEM> fitmes = new List<ITEM>();
            ITEM _item = new ITEM();
             _item.AMOUNT = "10";
            _item.RATE = "";
            _item.TYPE = "";
            _item.DESCRIPTION = "";
             fitmes.Add(_item);
              _fItem.ITEMS = fitmes;
            fn.ITEMIZEDCHARGES = _fItem;                                 
           return Request.CreateResponse(HttpStatusCode.OK, fn);
           }
         else
           {
            ANF fn = new ANF();
            return Request.CreateResponse(HttpStatusCode.OK, fn);
            } 
           }
          }
      else
       {
        return Request.CreateResponse(HttpStatusCode.BadRequest, abf);
       }
        
      }
      catch (Exception ex)
        {               
         return request.CreateResponse(HttpStatusCode.BadRequest);
         }
        }
A.Goutam
  • 3,422
  • 9
  • 42
  • 90
  • 1
    How do you create the XML output from your data? – Klaus Gütter Feb 06 '23 at 14:35
  • 1
    Can we see your serialization code? because this works fine and does *not* have the double-nesting of ``: `new XmlSerializer(_fItem.GetType()).Serialize(Console.Out, _fItem);`; so: what serializer are you using? – Marc Gravell Feb 06 '23 at 15:16
  • 1
    Please [edit] your question to include a [mcve], specifically your serialization code. Is there any chance you are using `DataContractSerializer`? You have applied [XmlSerializer attributes](https://learn.microsoft.com/en-us/dotnet/standard/serialization/attributes-that-control-xml-serialization) but if you are actually using `DataContractSerializer` they will have no effect. – dbc Feb 06 '23 at 15:52
  • The above code seems to work for me as well. Although since you're using a List, you may consider using `ITEMS` instead of `ITEM` (ex: `public List ITEMS { get; set; }`). For serialization/deserialization, the following may be helpful: https://stackoverflow.com/a/73640395/10024425 – Tu deschizi eu inchid Feb 06 '23 at 16:25
  • @KlausGütter i am retruning object to as response , – A.Goutam Feb 07 '23 at 05:26
  • Which object? Can you please show the whole method? – Klaus Gütter Feb 07 '23 at 05:28
  • 1
    Probably unrelated, but why do you declare `[ResponseType(typeof(IEnumerable))]` when in fact your method returns an `ANF`? – Klaus Gütter Feb 07 '23 at 06:42

0 Answers0