将xml文档读入XmlDocument对象

时间:2012-05-23 09:24:57

标签: xml vb.net web-services

我已经通过像这样的

这样的网络服务发布了xml文档
<WebMethod()> _
Public Function HelloWorld() As XmlDocument
    Dim xmlDoc As New XmlDocument
    xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory & "\Product.xml")
    Return xmlDoc
End Function

如何从其他Web服务将此Xml文档读入xmldocument对象?

1 个答案:

答案 0 :(得分: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)
相关问题