Answered by:
how should i return data in xml format from webservice

Question
-
User-1186779849 posted
[WebMethod]
[System.Web.Services.Protocols.SoapHeader("SoapHeader")]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public List<object> BorkertoRK()
{
List<object> propertyList = new List<object>();
if (!IsUserValid(SoapHeader))
propertyList.Add("Please call AuthenitcateUser() first.");
else
{
BrokertoRK Bnobj = new BrokertoRK();
Bnobj.BActionCode = SoapHeader.BActionCode;
Bnobj.BSuretyCode = SoapHeader.BSuretyCode;
Bnobj.BImporterName = SoapHeader.BImporterName;
Bnobj.BImporterAddressLine1 = SoapHeader.BImporterAddressLine1;
Bnobj.BImporterAddressLine2 = SoapHeader.BImporterAddressLine2;
Bnobj.BImporterAddressCity = SoapHeader.BImporterAddressCity;
Bnobj.BImporterAddressState = SoapHeader.BImporterAddressState;
Bnobj.BImporterAddressCountry = SoapHeader.BImporterAddressCountry;
Bnobj.BBondType = SoapHeader.BBondType;
Bnobj.BBondActivityCode = SoapHeader.BBondActivityCode;
Bnobj.BADCVDMerchandise = SoapHeader.BADCVDMerchandise;
Bnobj.BPortOfEntryCode = SoapHeader.BPortOfEntryCode;
Bnobj.BTransactionIdType = SoapHeader.BTransactionIdType;
Bnobj.BTransactionId = SoapHeader.BTransactionId;
Bnobj.BEntryType = SoapHeader.BEntryType;
Bnobj.BSTBBondProducerAccount = SoapHeader.BSTBBondProducerAccount;
Bnobj.BSecondaryNotifyParty = SoapHeader.BSecondaryNotifyParty;
Bnobj.BExceptionContactName = SoapHeader.BExceptionContactName;
Bnobj.BExceptionContactEmail = SoapHeader.BExceptionContactEmail;
Bnobj.BExceptionContactPhone = SoapHeader.BExceptionContactPhone;
Bnobj.BBrokerFilerReference = SoapHeader.BBrokerFilerReference;
Bnobj.BBondAmount = SoapHeader.BBondAmount;
Bnobj.BEstimatedEnteredValue = SoapHeader.BEstimatedEnteredValue;
//return "Hello " + HttpRuntime.Cache[SoapHeader.AuthenticatedToken];
foreach (var prop in Bnobj.GetType().GetProperties())
{
object value = prop.GetValue(Bnobj, new object[] { });
propertyList.Add(value);
}
}
return propertyList;
}Here i want xml output .
Tuesday, August 12, 2014 2:52 AM
Answers
-
User1918509225 posted
Hi krishore13,
Thanks for your post here.
For your problem, I am not sure what the property in your class BrokertoRK .
So I wrote a demo for you.
In my sample ,I have a class Employee .
I have create two method one is used to return xml format ,the other is used to return json format.
Employee.cs:
public class Employee { public string Name { get; set; } public string Company { get; set; } public string Address { get; set; } public string Phone { get; set; } public string Country { get; set; } }
Web Service.asmx:
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Xml)] public List<Employee> TestXML() { List<Employee> e = new List<Employee>(); // Employee[] e = new Employee[2]; Employee e1 = new Employee(); e1.Name = "Ajay Singh"; e1.Company = "Birlasoft Ltd."; e1.Address = "LosAngeles California"; e1.Phone = "310-235-1535"; e1.Country = "US"; Employee e2 = new Employee(); e2.Name = "Ajay Singh"; e2.Company = "Birlasoft Ltd."; e2.Address = "D-195 Sector Noida"; e2.Phone = "120-467500"; e2.Country = "India"; e.Add(e1); e.Add(e2); return e; } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string TestJSON() { Employee[] e = new Employee[2]; e[0] = new Employee(); e[0].Name = "Ajay Singh"; e[0].Company = "Birlasoft Ltd."; e[0].Address = "LosAngeles California"; e[0].Phone = "1204675"; e[0].Country = "US"; e[1] = new Employee(); e[1].Name = "Ajay Singh"; e[1].Company = "Birlasoft Ltd."; e[1].Address = "D-195 Sector Noida"; e[1].Phone = "1204675"; e[1].Country = "India"; return new JavaScriptSerializer().Serialize(e); }
When you view the web service in browser , click on the TestXML button,you will see the result is in xm format.
Hope it can help you.
Best Regards,
Kevin Shen.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 12, 2014 10:59 PM