Click here to Skip to main content
15,881,859 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a Web service, which returns the Response in XML. I am getting an error while reading the response if the response have empty fields.

I have tried different code for XML deserialization, but none of them is working.

I have below deserialization code written. I have tried this one and it was throwing server connection closed after receiving the response. Not sure what could be the issue please help me as early as possible.

What I have tried:

C#
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class T_OUTPUTItem
{

    [XmlElement("BUKRS")]
    public ushort BUKRS { get; set; }
    [XmlElement("WERKS")]
    public ushort WERKS { get; set; }
    [XmlElement("NAME1")]
    public string NAME1 { get; set; }
    [XmlElement("REGIO")]
    public string REGIO { get; set; }
    [XmlElement("MATKL")]
    public string MATKL { get; set; }
    [XmlElement("MATNR")]
    public ushort MATNR { get; set; }
    [XmlElement("LGORT")]
    public string LGORT { get; set; }
    [XmlElement("GRDIP")]
    public double? GRDIP {get; set;}
    [XmlElement("TRNDATE")]
    public System.DateTime TRNDATE { get; set; }
    [XmlElement("VOL_NATURAL")]
    public double? VOL_NATURAL
    {
        get
        {
            if (VOL_NATURAL == 0)
            {
                return 0;
            }
            else return VOL_NATURAL;
        }
    }

    [XmlElement("GRDIP_RUN")]
    public double? GRDIP_RUN
    {
        get
        {
            if (GRDIP_RUN == 0)
            {
                return 0;
            }
            else return GRDIP_RUN;
        }
    }
    [XmlElement("VOL_RUNNING")]
    public double? VOL_RUNNING
    {
        get
        {
            if (VOL_RUNNING == 0)
            {
                return 0;
            }
            else return VOL_RUNNING;
        }
    }

}
}


My Response will be like below

XML
<pre><ns0:YV_PIPELINE_PLANT_DIP_DETAILS.Response xmlns:ns0="urn:sap-com:document:sap:rfc:functions">
    <E_MSG/>
    <T_OUTPUT>
        <item>
            <BUKRS>2200</BUKRS>
            <WERKS>2222</WERKS>
            <NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
            <REGIO>OR</REGIO>
            <MATKL>BULK-MS</MATKL>
            <MATNR>16400</MATNR>
            <LGORT>PIPE</LGORT>
            <GRDIP/>
            <TRNDATE>2019-09-01</TRNDATE>
            <VOL_NATURAL>208.375</VOL_NATURAL>
            <GRDIP_RUN/>
            <VOL_RUNNING/>
        </item>
        <item>
            <BUKRS>2200</BUKRS>
            <WERKS>2222</WERKS>
            <NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
            <REGIO>OR</REGIO>
            <MATKL>BULK-MS</MATKL>
            <MATNR>16400</MATNR>
            <LGORT>T005</LGORT>
            <GRDIP>0212.8</GRDIP>
            <TRNDATE>2019-09-01</TRNDATE>
            <VOL_NATURAL>454.573</VOL_NATURAL>
            <GRDIP_RUN>0825.5</GRDIP_RUN>
            <VOL_RUNNING>1686.183</VOL_RUNNING>
        </item>
        <item>
            <BUKRS>2200</BUKRS>
            <WERKS>2222</WERKS>
            <NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
            <REGIO>OR</REGIO>
            <MATKL>BULK-MS</MATKL>
            <MATNR>16400</MATNR>
            <LGORT>T006</LGORT>
            <GRDIP>0188.0</GRDIP>
            <TRNDATE>2019-09-01</TRNDATE>
            <VOL_NATURAL>844.502</VOL_NATURAL>
            <GRDIP_RUN>0611.1</GRDIP_RUN>
            <VOL_RUNNING>2756.545</VOL_RUNNING>
        </item>
        <item>
            <BUKRS>2200</BUKRS>
            <WERKS>2222</WERKS>
            <NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
            <REGIO>OR</REGIO>
            <MATKL>BULK-MS</MATKL>
            <MATNR>16400</MATNR>
            <LGORT>T007</LGORT>
            <GRDIP>157.2</GRDIP>
            <TRNDATE>2019-09-01</TRNDATE>
            <VOL_NATURAL>699.171</VOL_NATURAL>
            <GRDIP_RUN>0159.3</GRDIP_RUN>
            <VOL_RUNNING>708.663</VOL_RUNNING>
        </item>
        <item>
            <BUKRS>2200</BUKRS>
            <WERKS>2222</WERKS>
            <NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
            <REGIO>OR</REGIO>
            <MATKL>BULK-MS</MATKL>
            <MATNR>16403</MATNR>
            <LGORT>PIPE</LGORT>
            <GRDIP/>
            <TRNDATE>2019-09-01</TRNDATE>
            <VOL_NATURAL>0</VOL_NATURAL>
            <GRDIP_RUN/>
            <VOL_RUNNING/>
        </item>
        <item>
            <BUKRS>2200</BUKRS>
            <WERKS>2222</WERKS>
            <NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
            <REGIO>OR</REGIO>
            <MATKL>BULK-SKO</MATKL>
            <MATNR>40000</MATNR>
            <LGORT>PIPE</LGORT>
            <GRDIP/>
            <TRNDATE>2019-09-01</TRNDATE>
            <VOL_NATURAL>99.572</VOL_NATURAL>
            <GRDIP_RUN/>
            <VOL_RUNNING/>
        </item>
        <item>
            <BUKRS>2200</BUKRS>
            <WERKS>2222</WERKS>
            <NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
            <REGIO>OR</REGIO>
            <MATKL>BULK-SKO</MATKL>
            <MATNR>40000</MATNR>
            <LGORT>T010</LGORT>
            <GRDIP>1427.9</GRDIP>
            <TRNDATE>2019-09-01</TRNDATE>
            <VOL_NATURAL>2190.395</VOL_NATURAL>
            <GRDIP_RUN>1606.9</GRDIP_RUN>
            <VOL_RUNNING>2464.582</VOL_RUNNING>
        </item>
        <item>
            <BUKRS>2200</BUKRS>
            <WERKS>2222</WERKS>
            <NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
            <REGIO>OR</REGIO>
            <MATKL>BULK-SKO</MATKL>
            <MATNR>40000</MATNR>
            <LGORT>T011</LGORT>
            <GRDIP>0283.3</GRDIP>
            <TRNDATE>2019-09-01</TRNDATE>
            <VOL_NATURAL>435.725</VOL_NATURAL>
            <GRDIP_RUN>1026.0</GRDIP_RUN>
            <VOL_RUNNING>1578.163</VOL_RUNNING>
        </item>
        <item>
            <BUKRS>2200</BUKRS>
            <WERKS>2222</WERKS>
            <NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
            <REGIO>OR</REGIO>
            <MATKL>BULK-HSD</MATKL>
            <MATNR>56000</MATNR>
            <LGORT>T001</LGORT>
            <GRDIP>1574.9</GRDIP>
            <TRNDATE>2019-09-01</TRNDATE>
            <VOL_NATURAL>8354.031</VOL_NATURAL>
            <GRDIP_RUN>1819.4</GRDIP_RUN>
            <VOL_RUNNING>9649.826</VOL_RUNNING>
        </item>
        <item>
            <BUKRS>2200</BUKRS>
            <WERKS>2222</WERKS>
            <NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
            <REGIO>OR</REGIO>
            <MATKL>BULK-HSD</MATKL>
            <MATNR>56000</MATNR>
            <LGORT>T002</LGORT>
            <GRDIP>334.2</GRDIP>
            <TRNDATE>2019-09-01</TRNDATE>
            <VOL_NATURAL>1770.146</VOL_NATURAL>
            <GRDIP_RUN>1819.5</GRDIP_RUN>
            <VOL_RUNNING>9649.669</VOL_RUNNING>
        </item>
        <item>
            <BUKRS>3300</BUKRS>
            <WERKS>3384</WERKS>
            <NAME1>IOT INFRASTRUCTURE & ENERGY SE</NAME1>
            <REGIO>CSG</REGIO>
            <MATKL>BULK-HSD</MATKL>
            <MATNR>56000</MATNR>
            <LGORT>T01D</LGORT>
            <GRDIP>222.8</GRDIP>
            <TRNDATE>2019-09-01</TRNDATE>
            <VOL_NATURAL>16610.006</VOL_NATURAL>
            <GRDIP_RUN/>
            <VOL_RUNNING/>
        </item>
    </T_OUTPUT>
</ns0:YV_PIPELINE_PLANT_DIP_DETAILS.Response>
Posted
Updated 11-Sep-19 21:02pm
Comments
[no name] 9-Sep-19 8:41am    
Maybe the serialization "worked" and it's the rest of your code that is the problem (since you show NO code that matters).

1 solution

I don't know how you're parsing the xml, what I can see is your mapping class does not seem to be in according with your swagger, try with this class

C#
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
	[XmlRoot(ElementName="item")]
	public class Item {
		[XmlElement(ElementName="BUKRS")]
		public string BUKRS { get; set; }
		[XmlElement(ElementName="WERKS")]
		public string WERKS { get; set; }
		[XmlElement(ElementName="NAME1")]
		public string NAME1 { get; set; }
		[XmlElement(ElementName="REGIO")]
		public string REGIO { get; set; }
		[XmlElement(ElementName="MATKL")]
		public string MATKL { get; set; }
		[XmlElement(ElementName="MATNR")]
		public string MATNR { get; set; }
		[XmlElement(ElementName="LGORT")]
		public string LGORT { get; set; }
		[XmlElement(ElementName="GRDIP")]
		public string GRDIP { get; set; }
		[XmlElement(ElementName="TRNDATE")]
		public string TRNDATE { get; set; }
		[XmlElement(ElementName="VOL_NATURAL")]
		public string VOL_NATURAL { get; set; }
		[XmlElement(ElementName="GRDIP_RUN")]
		public string GRDIP_RUN { get; set; }
		[XmlElement(ElementName="VOL_RUNNING")]
		public string VOL_RUNNING { get; set; }
	}

	[XmlRoot(ElementName="T_OUTPUT")]
	public class T_OUTPUT {
		[XmlElement(ElementName="item")]
		public List<Item> Item { get; set; }
	}

	[XmlRoot(ElementName="YV_PIPELINE_PLANT_DIP_DETAILS.Response", Namespace="urn:sap-com:document:sap:rfc:functions")]
	public class YV_PIPELINE_PLANT_DIP_DETAILS.Response {
		[XmlElement(ElementName="E_MSG")]
		public string E_MSG { get; set; }
		[XmlElement(ElementName="T_OUTPUT")]
		public T_OUTPUT T_OUTPUT { get; set; }
		[XmlAttribute(AttributeName="ns0", Namespace="http://www.w3.org/2000/xmlns/")]
		public string Ns0 { get; set; }
	}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900