2012-08-30 76 views
1

我有一個DataObjects類,其中包含一個包含int(EmailID)和一個字符串(EmailAddress)的UserEmail對象。將對象列表傳遞給Web服務

在C#.net應用程序中,如果我想顯示電子郵件地址列表 - 我創建並填充UserEmail對象列表。

List<DataObjects.UserEmails> myUserEmailsList = new List<DataObjects.UserEmails>(); 

並將其用作我碰巧使用的任何控件的數據源。

我需要將該列表傳遞給Web服務。我看不到如何做到這一點。如果另一方用一個將列表作爲參數的方法編寫Web服務 - 我可以調用Web服務並通過我的列表。但是他們如何能夠從列表中提取數據 - 而無需訪問在列表中創建對象的類?

有沒有一種方法循環對象列表而不知道對象的數據結構是什麼?

+1

你的屬性名的一個關鍵的電子郵件地址? – SliverNinja

+0

我將要使用的Web服務是REST,我被告知。 –

回答

1

當您使用其Web服務時,必須符合其數據結構。你把你的UserEmail對象數據,並將其轉換爲他們的服務所期待的對象。

如果您使用的服務只需要數據作爲獲取或發佈數據,則必須使用他們所需的任何密鑰。因此,他們可能要使用SOAP或REST協議爲Web服務使用採取「電子郵件」,而不是「EmailAddress的」

+0

因此,如果他們有一個名爲EmailList的對象具有EmailIdentifier和Email的屬性 - 我創建了一個UserEmail對象的副本,但是EmailList帶有EmailIdentifier和Email的屬性,而不是EmailID和EmailAddress的屬性? –

+0

我依賴於您正在訪問的Web服務的類型。你能否在你的問題中添加更多關於你要服用的服務的信息? – Gromer

+0

我還不知道......如果我無法弄清楚什麼是必需的,我想當我得到信息時我必須回來。 –

0
here a sample to pass list object to your webservice 

    <%@WebService Language="c#" class="CustomObjectArrayWS"%> 
using System; 
using System.Collections; 
using System.Web.Services; 
using System.Xml.Serialization; 
public class CustomObjectArrayWS 
{ 
     [WebMethodAttribute] 
     [XmlInclude(typeof(Address))] 
     public ArrayList GetAddresses() 
    { 
     ArrayList al = new ArrayList(); 
     Address addr1 = new Address("John Smith", "New York",12345); 
     Address addr2 = new Address("John Stalk", "San Fransisco", 12345); 

      al.Add(addr1); 
      al.Add(addr2); 

      return al; 
    } 
} 
// Custom class to be added to the collection to be passed in //and out of the service 
public class Address 
{ 
    public string name; 
    public string city; 
    public int zip;  
    // Default ctor needed by XmlSerializer 
    public Address() 
    { 
    } 
    public Address(string _name, string _city, int _zip ) 
    { 
        this.name = _name; 
        this.city = _city; 
        this.zip = _zip; 
      } 
     } 

看到http://www.programmersheaven.com/2/XML-Webservice-FAQ-Pass-Array-Of-Custom-Objects

+0

感謝您的幫助 - 我不能假裝我完全明白 - 我會試一試,看看我能否弄清楚。再次感謝。 –

+0

啊,一分錢一滴。現在得到它。再次感謝。 –

+0

感謝將答案標記爲好;-) –

相關問題