尝试使用C#从wsdl获取数据

时间:2019-01-02 21:54:13

标签: c# asp.net web-services wsdl

我有这个C#代码,我试图在Visual Studio中从wsdl获取数据。 代码还可以,但是当我尝试将数据写入记事本时,它只是显示一个空白:

WindowsService1.ServiceReference1.GetModifiedBookingsOperationResponse getModbkgsResp;
using (var proxy = new WindowsService1.ServiceReference1.InventoryServiceClient())
{
    int noofBookings = 1;
    getModbkgsResp = proxy.GetModifiedBookings(getModBkgsReq);
    WindowsService1.ServiceReference1.Booking[] bookings = new WindowsService1.ServiceReference1.Booking[noofBookings];
    getModbkgsResp.Bookings = new WindowsService1.ServiceReference1.Booking[noofBookings];
    getModbkgsResp.Bookings = bookings;
    if (getModbkgsResp.Bookings != null)
    {
        for (int i = 0; i < bookings.Length; i++)
        {

            Booking bk = new WindowsService1.ServiceReference1.Booking();
            getModbkgsResp.Bookings[i] = bk;
            if (bk != null )
            {
                bookingSource = bk.BookingSource;
                if (bk.BookingId == Bookingcode)
                {
                    this.WriteToFile("Booking Source =" + bookingSource + "");
                }
                else
                {
                    this.WriteToFile("Sorry could not find your source of booking");

                }
            }
            else
            {
                this.WriteToFile("Looks like source is null " );

            }
        }
    }
    else
    {
        this.WriteToFile("ERROR: Booking details not returned from GetModifiedBookings! " +StartDate);
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定您为什么使用new关键字来创建应该从服务中检索到的项目。自然,用new创建的任何内容都将使用默认值初始化,并且将不包含从服务中检索到的任何数据。

我的猜测是您的代码应更像这样:

using (var proxy = new WindowsService1.ServiceReference1.InventoryServiceClient())
{
    var response = proxy.GetModifiedBookings(getModBkgsReq);
    if (response.Bookings == null)
    {
        this.WriteToFile("ERROR: Booking details not returned from GetModifiedBookings! " +StartDate);
        return;
    }
    var booking = response.Bookings.SingleOrDefault( b => b.BookingId == bookingCode);
    if (booking == null)
    {
        this.WriteToFile("Sorry could not find your source of booking");
        return;
    }
    var bookingSource = booking.BookingSource;
    this.WriteToFile("Booking Source =" + bookingSource + "");
}