【问题标题】:Linq to SQL Web Service XMLLinq to SQL Web 服务 XML
【发布时间】:2010-03-31 15:36:26
【问题描述】:

我构建了一个连接到 SQL Server 数据库的 .NET Web 服务。有一个不会更改的 Web 服务调用 GetAllQuestions()。我将 GetAllQuestions 的结果保存到本地应用程序文件夹中的 GetAllQuestions.xml 并将其设置为内容。通常我会得到这样的网络服务结果:

var myService = new SATService();
var serviceQuestions = myService.GetAllQuestions();

我想尝试一下:

var serviceQuestions = File.Open("GetAllQuestions.xml");

非常感谢任何建议!

【问题讨论】:

    标签: c# sql-server xml web-services linq-to-sql


    【解决方案1】:

    GetAllQuestions.xml 很可能包含您并不真正需要的所有 SOAP 垃圾——我更喜欢标准的 .NET 序列化。

    如果您真的想将原始 SOAP 消息存储在 XML 文件中,请尝试 this

    【讨论】:

    • 我真的很想在本地使用 Web 服务结果,就像我进行远程调用一样。
    【解决方案2】:

    您不会说您的 Web 服务是 ASMX、兼容 ASP.NET 的 WCF 还是不兼容 ASP.NET 的 WCF。无论您的服务是如何托管的,此方法都应该为您提供文件所在的真实物理路径。

    string GetContentFilePath() {
        if(HttpRuntime.AppDomainAppVirtualPath != null) {
            // HTTP runtime is present, so the content file is in the virtual directory.
            return HttpRuntime.AppDomainAppPath;
        } else {
            // HTTP runtime is not present, so the content file is in the same directory as the assembly.
            return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        }
    }
    

    找到文件后,您现在需要考虑如何最好地阅读其内容。您有多种选择,例如LINQ to XML、XmlSerializer、XmlReader 甚至是类型化的 DataSet。最佳方法取决于文件的内容,因此您需要向我们提供更多详细信息。

    【讨论】:

    • Web 服务是 ASMX。如果保存为 xml 的 Web 服务结果自动转换为对象,我会很高兴。我不明白为什么 web 服务 GetAllQuestions() 方法可以立即变成对象,但是当它们是完全相同的东西时,xml 不能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多