简体   繁体   中英

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

This is my asp.net web service code, which returns an object of VremeZip class, it consists of 3 strings.

Now, I have created ac# windows forms app, and I want to read that object, and display all of the information provided.

I have a textbox, and a button with click event:

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

However, this will only return the first thing. For example, if I enter 90001 (Los Angeles zip code), it will return just the city in my label, instead of all three strings (name, temperature, url).

I can't create a new object in my windows forms and just do:

VremeZip v = new VremeZip();
v = v1.Vreme(textBox1.Text);

Since I get an error, because somehow my method returns string, not the actual object.

Is it somehow possible to get all 3 strings from the object?

Thanks for the tip t0mm13b, I didn't manage to return the whole object, I just override the tostring method and returned one string. From then, I split the string into seperate pieces.

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