【问题标题】:How to send an xml from a c# desktop application to a php server script and parse it?如何将 xml 从 c# 桌面应用程序发送到 php 服务器脚本并解析它?
【发布时间】:2011-10-23 03:28:29
【问题描述】:

在我的项目中,我想编写一个桌面应用程序,将 xml 文档作为文件或字符串发送, 到需要解析它的服务器上的 php 脚本。 现在,我有解析 xml 的 php 脚本,并在桌面应用程序中准备好 xml。 我的问题是: 一种。哪种方式更好:将文档作为文件还是作为字符串发送? 湾。如何用C#实现请求(文件或字符串),用php接受文件。

注意事项: 一种。我只能在桌面应用程序中使用 C#。 湾。我只能在服务器上使用 php 脚本。 C。我使用 System.xml.linq 在桌面应用程序中处理 xml 文档。

非常感谢!

【问题讨论】:

    标签: c# php .net xml desktop-application


    【解决方案1】:

    这可以像您喜欢的那样简单或复杂;-)
    这是一个非常简单的基本示例(没有错误处理等):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using System.Net;
    
    namespace LinqXMLTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Object[] res = { "Stackoverflow", true, 'x', 42};
                XElement xml = new XElement("Foo",
                    from a in res select
                        new XElement("bar",
                            new XElement("type", a.GetType()),
                            new XElement("value", a.ToString())
                        )
                );
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/test.php");
                request.Method = "POST";
                xml.Save( request.GetRequestStream() );
                HttpWebResponse resp = request.GetResponse() as HttpWebResponse;
            }
        }
    }
    

    作为“接收者”:

    <?php
    $fp = fopen('php://input', 'rb');
    file_put_contents('test.dat', $fp);
    

    服务器上的文件 test.dat 之后包含

    <?xml version="1.0" encoding="utf-8"?>
    <Foo>
      <bar>
        <type>System.String</type>
        <value>Stackoverflow</value>
      </bar>
      <bar>
        <type>System.Boolean</type>
        <value>True</value>
      </bar>
      <bar>
        <type>System.Char</type>
        <value>x</value>
      </bar>
      <bar>
        <type>System.Int32</type>
        <value>42</value>
      </bar>
    </Foo>
    

    另请参阅:
    XElement.Save Method (Stream)
    WebRequest.GetRequestStream Method
    http://docs.php.net/wrappers.php.php

    【讨论】:

      【解决方案2】:

      我对 C# 了解不多。但是您可以将 c# 中的 httprequest 发送到 php 文件并读取对变量的响应。这可以从您的 Windows 应用程序中完成。

      【讨论】:

      • 谢谢!我已经熟悉了httprequest,但我需要一个例子,以及服务器端的解释。
      【解决方案3】:

      我建议使用SOAP,因为它是您的同事/客户/合作伙伴/等众所周知的规范。阅读文档后也将能够理解。似乎比提出自己的规范更好。此外,还有许多其他人使用过 SOAP,因此查找示例应该很容易。

      以下是一些信息: W3C, Wikipedia

      【讨论】:

      • 谢谢,但你的建议是改变整个项目......不仅仅是我的部分。
      • 您需要在 C# 中创建一个 SOAP 信封并在 PHP 中打开它以获取您的 XML 文档。这不仅仅是按照 VolkerK 建议的方式发送它,而且我可以看到这对于一个小项目来说可能有点矫枉过正。不过,我仍然建议将它用于需要妥善记录的大型项目。使用现有标准而不是自己创建标准所付出的更多努力在编写文档时会获得多次回报。 :)
      • 谢谢!也许我会在未来的项目中使用你的建议。在那之前我需要学习那个协议。
      猜你喜欢
      • 2023-03-25
      • 2013-12-15
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 2014-01-29
      相关资源
      最近更新 更多