[英]Returning and reading objects in asp.net web service and windows forms
public class VremeZip : IXmlSerializable
{
string naziv;
string temperatura;
string zemljevid_url;
public void SetNaziv(string n)
{
naziv = n;
}
public void SetTemperatura(string n)
{
temperatura = n;
}
public void SetZemlj(string n)
{
zemljevid_url = n;
}
public string GetNaziv()
{
return naziv;
}
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}
public void WriteXml(XmlWriter writer)
{
writer.WriteElementString("string", naziv);
writer.WriteElementString("string", temperatura);
writer.WriteElementString("string", zemljevid_url);
}
}
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public VremeZip Vreme(string zip)
{
cdyne.Weather v1 = new cdyne.Weather();
VremeZip v = new VremeZip();
v.SetNaziv(v1.GetCityWeatherByZIP(zip).City);
v.SetTemperatura(v1.GetCityWeatherByZIP(zip).Temperature);
v.SetZemlj("ni urlja");
// return v1.GetCityWeatherByZIP(zip).Temperature;
return v;
}
}
這是我的asp.net Web服務代碼,它返回VremeZip類的對象,它由3個字符串組成。
現在,我已經創建了ac#Windows Forms應用程序,我想讀取該對象,並顯示提供的所有信息。
我有一個文本框和一個帶有click事件的按鈕:
public class VremeZip
{
public string naziv { get; set; }
public string temperatura { get; set; }
public string zemljevid_url { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
vreme.WebService1 v1 = new vreme.WebService1();
VremeZip v = new VremeZip();
label1.Text = v1.Vreme(textBox1.Text);
}
但是,這只會返回第一件事。 例如,如果我輸入90001(洛杉磯郵政編碼),它將僅返回標簽中的城市,而不是全部三個字符串(名稱,溫度,URL)。
我無法在Windows窗體中創建新對象,而只能這樣做:
VremeZip v = new VremeZip();
v = v1.Vreme(textBox1.Text);
由於出現錯誤,因為我的方法以某種方式返回了字符串,而不是實際的對象。
是否有可能從對象中獲取所有3個字符串?
感謝提示t0mm13b,我沒有設法返回整個對象,我只是重寫了tostring方法並返回了一個字符串。 從那時起,我將琴弦分割成不同的部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.