2012-05-23 132 views
2

我已经通过Web服务发布的XML文档类似这样的读XML文档转换成XmlDocument对象

<WebMethod()> _ 
Public Function HelloWorld() As XmlDocument 
    Dim xmlDoc As New XmlDocument 
    xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory & "\Product.xml") 
    Return xmlDoc 
End Function 

如何解读这个XML文档转换成XmlDocument的对象和其他Web服务?

回答

1

我根本不会使用XmlDocument作为返回类型。我建议干脆返回XML作为字符串,例如:

<WebMethod()> _ 
Public Function HelloWorld() As String 
    Return File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory & "\Product.xml") 
End Function 

然后,在你的客户端应用程序,您可以将XML字符串加载到XmlDocument对象:

Dim xmlDoc As XmlDocument = New XmlDocument() 
xmlDoc.LoadXml(serviceRef.HelloWorld()) 

但是,如果你需要保持方法返回一个XmlDocument,请记住它是一个复杂类型,所以在客户端,它将被表示为代理类型,而不是实际的XmlDocument类型。因此,您需要创建一个新的XmlDocument并从代理的xml文本中加载它:

Dim xmlDoc As XmlDocument = New XmlDocument() 
xmlDoc.LoadXml(serviceRef.HelloWorld().InnerXml) 
+0

非常感谢。它为我工作 –