0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
QLiu
  • 271
  • 2
  • 8
  • 23
  • Have you checked, using the debugger, what actual type is returned by GetXML()? Also, you don't need to initialize a new instance of XmlDocument as you're replacing it on the next line. You could try casting it explicitly xmltext = (XmlDocument)web2.GetXML() – Tomas McGuinness Apr 17 '12 at 09:25
  • 1
    have you changed `public object GetXML()` to `public XmlDocument GetXML()` ? if so you need to add reference to service again – Renatas M. Apr 17 '12 at 09:26
  • Hi Tomasmcguinness, it is public XmlDocument GetXML() XmlDocument already. – QLiu Apr 17 '12 at 09:45

2 Answers2

0

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();
    }
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0

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

Tyress
  • 3,573
  • 2
  • 22
  • 45