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

Trying to get data from wsdl using c#

我有这个 c# 代码,我试图从 visual studio 中的 wsdl 获取数据。 代码没问题,但是当我尝试将数据写入记事本时,它只显示一个空 space :

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);
    }
}

我不确定您为什么使用 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 + "");
}