如何从c#桌面应用程序发送xml到php服务器脚本并解析它


How to send an xml from a c# desktop application to a php server script and parse it?

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

指出:我只能在桌面应用程序中使用c#。b.我只能在服务器上使用php脚本。c.我使用System.xml.linq来处理桌面应用程序中的xml文档。

非常感谢!

这可以简单也可以复杂;-)
下面是一个非常简单的基本示例(没有错误处理等):

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。保存方法(流)
WebRequest。GetRequestStream方法
http://docs.php.net/wrappers.php.php

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

我建议使用SOAP,因为它是您的同事/客户/合作伙伴等所熟知的规范。将能够理解以及阅读文件后。这似乎是一个比提出自己的规范更好的选择。另外,还有很多人使用过SOAP,因此查找示例应该很容易。

这里是一些信息开始:W3C,维基百科