0

I am trying to write an application (simple form) that will consume(invoke) the web service (Service1.asmx) and display the results. Now, the web service has a method. Here is the code:

public class Service1 : System.Web.Services.WebService { [WebMethod] public Customer getCustomer(String id) { Customer customer = new Customer(); customer.CustomerId = id; customer.CustomerName = "ABC Warehouse"; customer.CustomerAddress = "123 Anywhere"; customer.CustomerCity = "Pittsburgh"; customer.CustomerState = "PA"; customer.CustomerZip = 10379; customer.CustomerContact = "Dan Smith"; customer.CustomerPhone = "2484567890"; customer.CustomerCredit = "True"; return customer; } } 

When I run the web service from its original project I am able to type text in the text box Example and click invoke to view the xml resultsExample. Now, the simple form I have in another project has a textbox (txt1), button (btn1), and label (lbl1). I successfully add the web service and all the functions and classes transfer. Now, what I want to happen is when you type something in the text box, click submit, and view the xml results in the label which will include the typed text from the text box, exactly like if I would of run the service on its own. Here is the code where I am having trouble:

 public partial class _Default : System.Web.UI.Page { protected void btn1_Click(object sender, EventArgs e) { MyService.Service1 service = new MyService.Service1(); string message = service.getCustomer(string id); ID = txt1.Text; lbl1.Text = message; } } 

Where am I going wrong? I am a beginner obviously, so all help would be appreciated. p.s.: MyService is what I named the namespace when I added the web service

3 Answers 3

3

your code will not compile because getCustomer return Customer object.

 protected void btn1_Click(object sender, EventArgs e) { MyService.Service1 service = new MyService.Service1(); MyService.Customer customer= service.getCustomer(string id); ID = customer.CustomerId; // here you can generate XML based on customer object if you really need to do so lbl1.Text = GetCustomerXML(customer);// implement method to get XML } private string GetCustomerXML( MyService.Customer customer) { XmlSerializer xsSubmit = new XmlSerializer(typeof(MyService.Customer)); StringWriter sw= new StringWriter(); XmlWriter writer = XmlWriter.Create(sw); xsSubmit.Serialize(writer, customer); return sw.ToString(); } 
Sign up to request clarification or add additional context in comments.

Comments

0

First of all in service method you need to define that response format data must be in XML format. and then in client use 'XmlNode' to get data from service. I think this post will be usefull for you

Comments

0

Your error is to be thinking that you're going to get XML back. You're not. You're going to get a MyService.Customer back.

FYI, you should be using "Add Service Reference" to consume the .asmx service.

2 Comments

So I am not going to get an XML layout back even though if I run the web service on its own it displays it in an xml layout? p.s.: I did use the .asmx service by going Add Service Reference -> Advanced -> Add Web Reference
What's an "XML Layout"? I suppose that if you really want XML, for some reason, you can serialize the MyService.Customer back into XML. Most people are glad to get a class instance and not a block of XML.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.