简体   繁体   中英

How can I create and return CSV, not XML, in an ASMX Web Service, using SOAP with Attachments?

Let's say I have a class like this:

class Person
{
    public string name;
    public string address;
    public string city;
    public string state;
    public string zip;
}

And I'm performing a data dip on a database:

Select Name, Address, City, State, Zip
FROM Persons_Tbl

I am storing the data in in the Person class, via SqlDataReader.

Normally, the Web Service will return the data and it'll be XML formatted. However, I want to return it as CSV, not XML. I realize that web services return either XML or JSON. But there is something called SOAP with Attachments. Can the attachment be CSV and, if so, how could I do this?

SOAP with Attachments (SwA) was never standardized:

SwA is a W3C Note. It was submitted as a proposal, but it was not adopted by the W3C. Instead, MTOM is the W3C Recommendation for handling binary data in SOAP messages.

In any case, the legacy ASMX web service technology supported neither SwA nor MTOM. Is there some reason you can't use WCF? It does support MTOM.

Are you looking for the Web Service call to send a file, or you only care about the output being a CSV?

If you only need a CSV formatted output, you can send the CSV as a string and then save it to a file in the application that consumes the web service:

string csvFile = webService.GetCSV()
File.WriteAllText(filePath, csvFile );

Do note that it will still return XML, but your data (string) would be in CSV format. You will have to create some helper methods to take your object and convert it into CSV format.

There are a few other implementations that are based on how you plan on using that web service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM