在我的项目中,我想编写一个桌面应用程序,该应用程序以文件或字符串的形式发送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.Save Method (Stream)
WebRequest.GetRequestStream Method
http://docs.php.net/wrappers.php.php