我需要在REST WCF服务中测试一个.Net方法,它需要传入一个DataTable并返回一个DataTable:
DataTable TryPostDataTable(DataTable dataTable);
在我的Java测试代码中,我使用HtttClient向此服务发送Post请求。它有标题:
"Content-Type", "text/xml; charset=utf-8"
和身体:
<TryPostDataTable xmlns="http://tempuri.org/">
<dataTable>
<Person>
<name>A Name</name>
<age>An Age</age>
</Person>
</dataTable>
</TryPostDataTable>
但是当服务收到请求时,dataTable变量总是变成一个空的DataTable实例,没有行和列
如果我将dataTable的类型更改为String,并将Post主体更改为:
<TryPostDataTable xmlns="http://tempuri.org/">
<datatable>
TestString
</datatable>
</TryPostDataTable>
然后它正确解析请求。
我无法弄清楚如何通过HttpPost传递DataTable对象。 Rest服务中从XML到DataTable的解析过程有什么问题?或者我的帖子不正确?
任何帮助都将非常感激。
//编辑1:
我尝试使用此代码生成xml内容:
DataTable dtEmployee = new DataTable("Person");
dtEmployee.Columns.Add("Name", typeof(string));
dtEmployee.Columns.Add("Age", typeof(string));
dtEmployee.Rows.Add("A Name", "An Age");
dtEmployee.WriteXml(@"C:\Working\Employee.xml", XmlWriteMode.WriteSchema);
将生成:
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Person" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="Age" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
<Person>
<Name>A Name</Name>
<Age>An Age</Age>
</Person>
然后我使用httpPost将此xml发送到TryPostDataTable方法。
服务现在成功解析模式并生成“Person”表,其中2列“Name” - “Age”,分配给变量dataTable。但没有行 - 内部没有数据。
答案 0 :(得分:0)
我在服务器上有我的REST方法,如下所示:
[OperationContract]
[WebInvoke]
public DataTable GetMyDataTable(DataTable dt)
{
DataTable returnTable = new DataTable("ServerTable");
returnTable.Columns.Add(new DataColumn() { ColumnName = "FirstName", DataType = typeof(String)});
returnTable.Columns.Add(new DataColumn() { ColumnName = "LastName", DataType = typeof(String) });
if (dt.Columns.Count == 2 && dt.Rows.Count > 0)
{
return returnTable;
}
else
{
returnTable.Columns.Add(new DataColumn() { ColumnName = "MiddleName", DataType = typeof(String) });
return returnTable;
}
}
调用Web服务时的原始请求应如下所示:
POST http://localhost/SampleService/Service1.svc/GetMyDataTable HTTP/1.1
Content-Type: application/xml
Content-Length: 1022
Host: rajeshwin7
<DataTable xmlns="http://schemas.datacontract.org/2004/07/System.Data"><xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"><xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="SampleTable" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="SampleTable"><xs:complexType><xs:sequence><xs:element name="FirstName" type="xs:string" minOccurs="0"/><xs:element name="LastName" type="xs:string" minOccurs="0"/></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"><DocumentElement xmlns=""><SampleTable diffgr:id="SampleTable1" msdata:rowOrder="0" diffgr:hasChanges="inserted"><FirstName>MyFirst</FirstName><LastName>MyLast</LastName></SampleTable></DocumentElement></diffgr:diffgram></DataTable>
我从服务器返回的响应如下:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 804
Content-Type: application/xml; charset=utf-8
Date: Wed, 14 Nov 2012 17:07:37 GMT
<DataTable xmlns="http://schemas.datacontract.org/2004/07/System.Data"><xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"><xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="ServerTable" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="ServerTable"><xs:complexType><xs:sequence><xs:element name="FirstName" type="xs:string" minOccurs="0"/><xs:element name="LastName" type="xs:string" minOccurs="0"/></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"/></DataTable>
我对Fiddler捕获的原始请求和响应进行了POST。只需监控您的请求的外观,这应该可以帮助您毫无问题地访问REST服务。此外,数据表应该有一个名称,否则它将无法序列化和反序列化。
答案 1 :(得分:0)
最好将整个表转换为List 像这样,我用过: -
public List<user> fulldata()
{
string ConnectString = "server=localhost;database=test_local;integrated security=SSPI";
string QueryString = "select * from tbl_register";
SqlConnection myConnection = new SqlConnection(ConnectString);
SqlDataAdapter da = new SqlDataAdapter(QueryString, myConnection);
DataSet ds = new DataSet();
da.Fill(ds, "Register Table");
List<user> ls = new List<user>();
foreach (DataRow row in ds.Tables[0].Rows)
{
user res = new user();
res.name = row["name"].ToString();
res.company = row["cname"].ToString();
res.address = row["address"].ToString();
ls.Add(res);
}
return ls;
}
public class user
{
public string name { get; set; }
public string company { get; set; }
public string address { get; set; }
}