QLiu
QLiu

Reputation: 271

Read XML response webservice

I am so new to .Net web service programming. I am running into a trouble to read the XML response from web services to my client.

In my webservice side: Service1.asmx.cs code:

    [WebMethod(Description = "substruction")]
    public double subtract(double i, double j)
    {
        return i - j;
    }


    [WebMethod(Description = "getxml")]
    public XmlDocument GetXML()
    {
        StringBuilder sb = new StringBuilder();
        XmlWriter writer = XmlWriter.Create(sb);

        writer.WriteStartDocument();
        writer.WriteStartElement("People");

        writer.WriteStartElement("Person");
        writer.WriteAttributeString("Name", "Nick");
        writer.WriteEndElement();

        writer.WriteStartElement("Person");
        writer.WriteStartAttribute("Name");
        writer.WriteValue("Kevin");
        writer.WriteEndAttribute();
        writer.WriteEndElement();

        writer.WriteEndElement();
        writer.WriteEndDocument();

        writer.Flush();

        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(sb.ToString());
        return xmlDocument;

    }

In here, i create two methods to test the response. In GetXML, i create a very simple XML and return a package XML to client.

In my client side:

    // Add button click function 
    protected void Button1_Click(object sender, EventArgs e)
    {
        string selectFlag = selectOper.Value;
        localhost.Service1 web = new localhost.Service1(); // Have to be the same name as youre Service1. 
        if (selectFlag.Equals("+"))
        {
            Result.Text = (web.addition(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
        }
        else if (selectFlag.Equals("-"))
        {
            Result.Text = (web.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
        }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {


        localhost.Service1 web2 = new localhost.Service1(); // Can u please do not be so silly, use different instance name here. 
        Button clickedButton = (Button)sender;

        XmlDocument xmltest = new XmlDocument();
        xmltest = web2.GetXML();

You can see i tried to get web2.GetXML() get the whole XML into XmlDoucment. However, it said Error 1 Cannot implicitly convert type 'object' to 'System.Xml.XmlDocument'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\qili\My Documents\Downloads\WebService3\WebService2\WebService2\Default.aspx.cs 39 24 WebService2

Any tips, i guess i am doing something wrong. But the Buttom1_Click method is working fine.

Upvotes: 0

Views: 14005

Answers (2)

Tyress
Tyress

Reputation: 3653

You should just try deleting the service reference, rerun the service, and on the client add service reference.

Upvotes: 0

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28588

try to convert the response of service:

protected void Button2_Click(object sender, EventArgs e)
    {


     localhost.Service1 web2 = new localhost.Service1(); // Can u please do not be so silly, use different instance name here. 
     Button clickedButton = (Button)sender;
     XmlDocument xmltest = new XmlDocument();
     xmltest = (XmlDocument)web2.GetXML();
    }

Upvotes: 0

Related Questions