C# 从Web服务返回XML

C# 从Web服务返回XML,c#,.net,xml,vb.net,web-services,C#,.net,Xml,Vb.net,Web Services,我有一个XML文件,它位于运行Web服务的服务器的硬盘上。我需要从另一个应用程序访问该文件 这是我在Web服务上的方法 Public Function getXMLFile() Dim xmlDocument As System.Xml.XmlDocument xmlDocument = New System.Xml.XmlDocument() xmlDocument.Load("C:\Sommaire.xml") Return xmlDocument End

我有一个XML文件,它位于运行Web服务的服务器的硬盘上。我需要从另一个应用程序访问该文件

这是我在Web服务上的方法

Public Function getXMLFile()
    Dim xmlDocument As System.Xml.XmlDocument

    xmlDocument = New System.Xml.XmlDocument()
    xmlDocument.Load("C:\Sommaire.xml")

    Return xmlDocument
End Function
当我导航到我的Web服务并尝试调用我的方法时,出现以下错误:

System.InvalidOperationException:生成 XML文档。-->System.InvalidOperationException:类型 System.Xml.XmlDocument不能在此上下文中使用

这是在尝试返回
xmlDocument
对象时造成的

从我收集的信息来看,就像SOAP想用更多的XML来包装我的XML,并阻止我这样做


如果无法返回XML,如何从Web服务获取XML文件?

您的函数没有指定返回类型,但您正在尝试返回System.XML.XmlDocument类型的对象

改变

Public Function getXMLFile() 

应为的整个代码段:

Public Function getXMLFile()  AS System.Xml.XmlDocument
    Dim xmlDocument As System.Xml.XmlDocument

    xmlDocument = New System.Xml.XmlDocument()
    xmlDocument.Load("C:\Sommaire.xml")

    Return xmlDocument
End Function

@PeekaySwitch:您应该将
选项严格地打开
,这样您就不会有这种问题了。@johnsaundersooh,谢谢!我以后会记住这一点!
Public Function getXMLFile()  AS System.Xml.XmlDocument
    Dim xmlDocument As System.Xml.XmlDocument

    xmlDocument = New System.Xml.XmlDocument()
    xmlDocument.Load("C:\Sommaire.xml")

    Return xmlDocument
End Function