我正在关注大学项目的教程,并且我遇到了一些障碍,所以非常感谢有助于我理解的任何见解。
我正在尝试编写一个非常简单的VB表单客户端/ asp.net Web服务,它允许客户端从服务中读取一些XML。
在服务上我有以下代码(不包括将xml文件加载到服务中的代码)
Private Function getAllPlayList(ByVal userID As String) As XmlElement
Dim root As XmlElement
Dim xPath As String = "//User[starts-with(@ID, '" & userID & "')]"
root = xmlDataPlaylists.DocumentElement.SelectSingleNode(xPath)
Return root
End Function
<WebMethod()> _
Public Function GetPlaylists(ByVal userID As String) As String
Return getAllPlayList(userID).OuterXml
End Function
在运行服务和调用时似乎起作用,产生:
<string xmlns="http://tempuri.org/"> <User ID="B23785"><Playlist ID="Rock"><Song name="Test Song"><Album>Test Album 1</Album><Artist>Test Artist 1</Artist><Title>Test Song 1</Title><Favourite>False</Favourite></Song><Song name="Test Song 2"><Album>Test Album 2</Album><Artist>Test Artist 2</Artist><Title>Test Song 2</Title><Favourite>False</Favourite></Song></Playlist><Playlist ID="POP"><Song name="Test Song"><Album>Rock Album 1</Album><Artist>Rock Artist 1</Artist><Title>Rock Song 1</Title><Favourite>False</Favourite></Song><Song name="Test Song 2"><Album>Rock Album 2</Album><Artist>Rock Artist 2</Artist><Title>Rock Song 2</Title><Favourite>False</Favourite></Song></Playlist></User> </string>
就这一点而言,至少似乎按预期执行。我遇到的麻烦是试图让客户端接收并加载相同的XML。我的代码是:
Private userID As String
Private playList As Xml.XmlElement
Private service As listService.ServiceSoapClient
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
userID = "B23785"
service = New listService.ServiceSoapClient
getPlaylists()
End Sub
Private Sub getPlaylists()
Dim playListDoc As Xml.XmlDocument
playListDoc = New Xml.XmlDocument()
playListDoc.LoadXml(service.GetPlaylists(userID))
End Sub
问题似乎在于:
playListDoc.LoadXml(service.GetPlaylists(用户ID))
我认为会在这个新的XML文档中加载从服务返回的字符串值。相反,我得到错误:
XmlReader必须位于Element类型的节点上,而不是Text
的节点上这让我非常困惑,所以如果有人能够阐明为什么这不起作用我将非常感激!谢谢你的时间
答案 0 :(得分:1)
在wcf服务(vanilla visual studio模板)中,我创建了这个Web服务
public XmlElement testme()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<foo><bar name=\"hello\"/></foo>");
string xpath = "//bar";
XmlElement e = doc.DocumentElement.SelectSingleNode(xpath) as XmlElement;
return e;
}
在控制台应用程序中,我创建了一个vanilla服务引用,并调用了服务
Service1Client c = new Service1Client();
XElement e = c.testme();
几乎可以通过网络对其进行SOAP处理并将XmlElement转换为XElement,因为XElement是MS xml api的下一个版本,也就是VS Web服务代码生成器的功能。
所以你可以这样做并称之为一天,抱歉我的C#,我没有hablo VB :-D