2012-04-17 86 views
0

我對.Net web服務編程如此陌生。我遇到麻煩,要從Web服務讀取XML響應給我的客戶端。閱讀XML響應webservice

在我的web端:Service1.asmx.cs代碼:

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

    } 

在這裏,我創建了兩個方法來測試的響應。在GetXML中,我創建了一個非常簡單的XML並將包XML返回給客戶端。

在我的客戶端:

// 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(); 

你可以看到我試圖讓web2.GetXML()得到整個XML轉換XmlDoucment。但是,它表示錯誤1不能將類型'object'隱式轉換爲'System.Xml.XmlDocument'。存在明確的轉換(您是否缺少轉換?)C:\ Documents and Settings \ qili \ My Documents \ Downloads \ WebService3 \ WebService2 \ WebService2 \ Default.aspx.cs 39 24 WebService2

任何提示,我想我我做錯了什麼。但Buttom1_Click方法工作正常。

+0

您是否使用調試器檢查了GetXML()返回的實際類型?此外,您不需要在下一行替換它時初始化XmlDocument的新實例。您可以嘗試顯式地將其轉換爲xmltext =(XmlDocument)web2.GetXML() – tomasmcguinness 2012-04-17 09:25:23

+1

您是否已將公共對象GetXML()更改爲public XmlDocument GetXML()?如果是這樣,你需要再次添加對服務的引用 – Reniuz 2012-04-17 09:26:54

+0

嗨Tomasmcguinness,它是公共的XmlDocument GetXML()XmlDocument已經。 – QLiu 2012-04-17 09:45:55

回答

0

嘗試服務的響應轉換:

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(); 
    } 
+0

嗨,當我點擊按鈕,異常發生; NotImplementedException是用戶代碼未處理的。在xmltest =(XmlDocument_web2.GetXML(); – QLiu 2012-04-17 09:36:29

+0

)您應該調試您是否能夠訪問代碼 – 2012-04-17 09:40:52

+0

我試圖調試它,我不能通過使用http:// localhost:50228/Service1直接訪問XML。 asmx/GetXML – QLiu 2012-04-17 09:57:01

0

你應該只嘗試刪除服務引用,重新運行該服務,並在客戶端上添加服務引用。