我从“ServiceOperation”(WCF数据服务)返回客户端:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xml:base="http://dd-1620/SampleData.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<id>http://dd-1620/SampleData.svc/Customers('1001')</id>
<title type="text"></title>
<updated>2011-06-21T18:42:05Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Customer" href="Customers('1001')" />
<category term="Customer" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:MasterCustomerId>1001</d:MasterCustomerId>
<d:SubCustomerId>0</d:SubCustomerId>
<d:FirstName>Jag</d:FirstName>
<d:LastName>Chat</d:LastName>
</m:properties>
</content>
</entry>
我们在执行以下操作时得到以上内容:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sUri);
req.Credentials = cache;
req.Method = "POST";
req.ContentType = "application/xml";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream respStream = resp.GetResponseStream();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string sResp = sr.ReadToEnd();
sr.Close();
当我尝试使用以下内容反序列化它时:
DataContractSerializer dc = new DataContractSerializer(typeof(Customer));
return (Customer)dc.ReadObject(new System.IO.MemoryStream(sResp.ToByteArrayUTF8()));
我收到以下错误:
第1行位置错误249.期望来自名称空间'http://schemas.datacontract.org/2004/07/SampleSvcRef'的元素'Customer'。遇到名为'entry'的'Element',名称空间'http://www.w3.org/2005/Atom'。
更新:
使用服务引用生成的Customer类如下:
[global::System.Data.Services.Common.EntitySetAttribute("Customers")]
[global::System.Data.Services.Common.DataServiceKeyAttribute("MasterCustomerId")]
public partial class Customer : global::System.ComponentModel.INotifyPropertyChanged
{
/// <summary>
/// Create a new Customer object.
/// </summary>
/// <param name="masterCustomerId">Initial value of MasterCustomerId.</param>
/// <param name="subCustomerId">Initial value of SubCustomerId.</param>
/// <param name="lastName">Initial value of LastName.</param>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public static Customer CreateCustomer(string masterCustomerId, string subCustomerId, string lastName)
{
Customer customer = new Customer();
customer.MasterCustomerId = masterCustomerId;
customer.SubCustomerId = subCustomerId;
customer.LastName = lastName;
return customer;
}
/// <summary>
/// There are no comments for Property MasterCustomerId in the schema.
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string MasterCustomerId
{
get
{
return this._MasterCustomerId;
}
set
{
this.OnMasterCustomerIdChanging(value);
this._MasterCustomerId = value;
this.OnMasterCustomerIdChanged();
this.OnPropertyChanged("MasterCustomerId");
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private string _MasterCustomerId;
partial void OnMasterCustomerIdChanging(string value);
partial void OnMasterCustomerIdChanged();
/// <summary>
/// There are no comments for Property SubCustomerId in the schema.
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string SubCustomerId
{
get
{
return this._SubCustomerId;
}
set
{
this.OnSubCustomerIdChanging(value);
this._SubCustomerId = value;
this.OnSubCustomerIdChanged();
this.OnPropertyChanged("SubCustomerId");
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private string _SubCustomerId;
partial void OnSubCustomerIdChanging(string value);
partial void OnSubCustomerIdChanged();
/// <summary>
/// There are no comments for Property FirstName in the schema.
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string FirstName
{
get
{
return this._FirstName;
}
set
{
this.OnFirstNameChanging(value);
this._FirstName = value;
this.OnFirstNameChanged();
this.OnPropertyChanged("FirstName");
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private string _FirstName;
partial void OnFirstNameChanging(string value);
partial void OnFirstNameChanged();
/// <summary>
/// There are no comments for Property LastName in the schema.
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string LastName
{
get
{
return this._LastName;
}
set
{
this.OnLastNameChanging(value);
this._LastName = value;
this.OnLastNameChanged();
this.OnPropertyChanged("LastName");
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
private string _LastName;
partial void OnLastNameChanging(string value);
partial void OnLastNameChanged();
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public event global::System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
protected virtual void OnPropertyChanged(string property)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new global::System.ComponentModel.PropertyChangedEventArgs(property));
}
}
}
感谢
答案 0 :(得分:0)
Customer
对象需要看起来像这样。
[MetadataType(typeof(MyEntities.Customer))]
[DataContract(Name = "entry")]
public partial Customer
{
[DataMember(Name = "id")]
public string Id { get; set; }
[DataMember(Name = "title")]
public string Title { get; set; }
}
结帐此链接http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx
答案 1 :(得分:-1)