Person类的定义:
Public Class Person
Inherits System.Attribute
Public Name As String
Public Age As String
End Class
我们可以看到在本地Person的代理类:
'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://tempuri.org/ExcelService/Service1")> _
Public Class Person
Inherits Attribute
'<remarks/>
Public Name As String
'<remarks/>
Public Age As String
End Class
'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://tempuri.org/ExcelService/Service1"), _
System.Xml.Serialization.XmlIncludeAttribute(GetType(Person))> _
Public MustInherit Class Attribute
End Class
只要你的类继承于System.Attribute就可以了。
例子:
<WebMethod()> _
Public Function DisplayPersonInfo(ByVal objPerson As Person) As String
Dim person As Person = objPerson
Return person.Name
End Function
调用:
Dim myWebSvc As WebSvc.PA0010SVC = New WebSvc.PA0010SVC
Dim p As WebSvc.Person = New WebSvc.Person
p.Name = "Peter"
p.Age = "23"
Dim strPersonName As String = "null"
strPersonName = myWebSvc.DisplayPersonInfo(p)
将自定义类在webservice里序列化成字符串,传到客户端,然后客户端再反序列化成类的实例。
自定义类:
public class myclass
{
[XmlElement]
public string str;
}
web服务:
public string getclass()
{
myclass mc = new myclass();
XmlSerilazier xs = new XmlSerilazier(typeof(myclass));
MemoryStream ms = new MemoryStream();
xs.Serialize(mc,ms);
ms.Seek(0,SeekOrigin.Begin);
byte[] buffer = new byte[ms.Length];
int n=ms.Read(buffer,0,buffer.Length);
ms.Close();