webservice,服务器端获取数据,并转化为xml格式,供客户端调用 200
webservice服务器端,定义一个服务,从数据库获取数据,并转化为xml,供客户端调用。实例化客户端之后,调用这个接口,将获取的xml在控制台输出。...
webservice服务器端,定义一个服务,从数据库获取数据,并转化为xml,供客户端调用。
实例化客户端之后,调用这个接口,将获取的xml在控制台输出。 展开
实例化客户端之后,调用这个接口,将获取的xml在控制台输出。 展开
1个回答
2016-06-23
展开全部
HttpContext.Current.Request.InputStream 就可以读出Xml内容。
// 创建一个DataTable
DataTable data = new DataTable("Project");
data.Columns.Add("Name");
data.Columns.Add("Birthday");
data.Rows.Add(new object[] { "Wendy", "1978/03/11" });
data.Rows.Add(new object[] { "Philip", "2000/11/05" });
data.Rows.Add(new object[] { "Felix", "1999/08/04" });
using (var ms = new MemoryStream())
{
// 将DataTable用Xml格式写入流
data.WriteXml(ms, XmlWriteMode.WriteSchema);
var client = new WebClient();
// 定义HttpRequest的Content-Type(xml,json等)
client.Headers.Add("Content-Type", "text/xml");
var url = ";
// Send HttpRequest
var resp = client.UploadData(url, "POST", ms.ToArray());
var strResp = System.Text.Encoding.UTF8.GetString(resp);
MessageBox.Show(strResp);
}
服务端:
[c-sharp] view plaincopy
[WebMethod]
public void SendXml()
{
// 获得客户端RAW HttpRequest
var inputStream = HttpContext.Current.Request.InputStream;
// 定义Response返回的格式是:Json
var response = HttpContext.Current.Response;
response.ContentType = "text/json";
//var strXml = "";
//using (var sr = new StreamReader(inputStream))
// strXml = sr.ReadToEnd();
try
{
DataTable data = new DataTable();
using (var xr = XmlReader.Create(inputStream))
data.ReadXml(xr);
// 将读入Xml的DataTable的行数返回客户端
string count = "/"" + data.Rows.Count + "/"";
response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(count));
}
catch (Exception ex)
{
response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(ex.Message));
}
}
// 创建一个DataTable
DataTable data = new DataTable("Project");
data.Columns.Add("Name");
data.Columns.Add("Birthday");
data.Rows.Add(new object[] { "Wendy", "1978/03/11" });
data.Rows.Add(new object[] { "Philip", "2000/11/05" });
data.Rows.Add(new object[] { "Felix", "1999/08/04" });
using (var ms = new MemoryStream())
{
// 将DataTable用Xml格式写入流
data.WriteXml(ms, XmlWriteMode.WriteSchema);
var client = new WebClient();
// 定义HttpRequest的Content-Type(xml,json等)
client.Headers.Add("Content-Type", "text/xml");
var url = ";
// Send HttpRequest
var resp = client.UploadData(url, "POST", ms.ToArray());
var strResp = System.Text.Encoding.UTF8.GetString(resp);
MessageBox.Show(strResp);
}
服务端:
[c-sharp] view plaincopy
[WebMethod]
public void SendXml()
{
// 获得客户端RAW HttpRequest
var inputStream = HttpContext.Current.Request.InputStream;
// 定义Response返回的格式是:Json
var response = HttpContext.Current.Response;
response.ContentType = "text/json";
//var strXml = "";
//using (var sr = new StreamReader(inputStream))
// strXml = sr.ReadToEnd();
try
{
DataTable data = new DataTable();
using (var xr = XmlReader.Create(inputStream))
data.ReadXml(xr);
// 将读入Xml的DataTable的行数返回客户端
string count = "/"" + data.Rows.Count + "/"";
response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(count));
}
catch (Exception ex)
{
response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(ex.Message));
}
}
更多追问追答
追问
不是要读出xml…而是要将从数据库中用mybatis查出来的数据…转化为xml,供客户端调用…在客户到调用…将其在控制台输出就好了…
这是个课题…
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |