C# 如何反序列化WCF数据服务对象?

C# 如何反序列化WCF数据服务对象?,c#,serialization,wcf-data-services,C#,Serialization,Wcf Data Services,我已将以下内容从“ServiceOperation”(WCF数据服务)返回给客户端: 当我尝试使用以下命令对其进行反序列化时: HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sUri); req.Credentials = cache; req.Method = "POST"; req.ContentType = "application/xml"; HttpWebResponse resp = (HttpWebResponse)r

我已将以下内容从“ServiceOperation”(WCF数据服务)返回给客户端:

当我尝试使用以下命令对其进行反序列化时:

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处出现错误。应为命名空间中的元素“Customer”http://schemas.datacontract.org/2004/07/SampleSvcRef'.. 遇到名称为“entry”的“Element”,命名空间为http://www.w3.org/2005/Atom“。

更新:

使用服务引用生成的客户类如下所示:

[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));
        }
    }
}
[global::System.Data.Services.Common.EntitySetAttribute(“客户”)]
[global::System.Data.Services.Common.DataServiceKeyAttribute(“MasterCustomerId”)]
公共部分类客户:全局::System.ComponentModel.INotifyPropertyChanged
{
/// 
///创建一个新的客户对象。
/// 
///MasterCustomerId的初始值。
///子客户ID的初始值。
///LastName的初始值。
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Data.Services.Design”,“1.0.0”)]
公共静态客户CreateCustomer(字符串masterCustomerId、字符串subCustomerId、字符串lastName)
{
客户=新客户();
customer.MasterCustomerId=MasterCustomerId;
customer.SubCustomerId=SubCustomerId;
customer.LastName=LastName;
退货客户;
}
/// 
///架构中没有属性MasterCustomerId的注释。
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Data.Services.Design”,“1.0.0”)]
公共字符串主自定义ID
{
收到
{
返回此。\u MasterCustomerId;
}
设置
{
此.OnMasterCustomerIdChanging(值);
这。_MasterCustomerId=值;
this.OnMasterCustomerIdChanged();
此.OnPropertyChanged(“主客户ID”);
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Data.Services.Design”,“1.0.0”)]
私有字符串\u MasterCustomerId;
主自定义更改部分无效(字符串值);
MasterCustomerIdChanged()上的部分无效;
/// 
///架构中没有属性SubCustomerId的注释。
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Data.Services.Design”,“1.0.0”)]
公共字符串子客户ID
{
收到
{
返回此。\u子客户ID;
}
设置
{
此.onSubCustomerIdChange(值);
此._SubCustomerId=值;
this.OnSubCustomerIdChanged();
此.OnPropertyChanged(“子客户ID”);
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Data.Services.Design”,“1.0.0”)]
私有字符串\u子客户ID;
SubCustomerIdChange上的部分无效(字符串值);
SubCustomerIdChanged()上的部分无效;
/// 
///架构中没有属性FirstName的注释。
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Data.Services.Design”,“1.0.0”)]
公共字符串名
{
收到
{
返回此。\u FirstName;
}
设置
{
此.onfirstNameChanged(值);
这个。_FirstName=value;
this.OnFirstNameChanged();
本协议项下。OnPropertyChanged(“名字”);
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Data.Services.Design”,“1.0.0”)]
私有字符串_FirstName;
部分无效OnFirstName更改(字符串值);
部分无效OnFirstNameChanged();
/// 
///架构中没有属性LastName的注释。
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Data.Services.Design”,“1.0.0”)]
公共字符串姓氏
{
收到
{
返回此。\u LastName;
}
设置
{
此.onLastNameChanged(值);
这是。_LastName=value;
this.OnLastNameChanged();
此.OnPropertyChanged(“姓氏”);
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Data.Services.Design”,“1.0.0”)]
私有字符串_LastName;
部分无效OnLastNameChanged(字符串值);
部分void OnLastNameChanged();
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Data.Services.Design”,“1.0.0”)]
公共事件全局::System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Data.Services.Design”,“1.0.0”)]
受保护的虚拟void OnPropertyChanged(字符串属性)
{
如果((this.PropertyChanged!=null))
{
this.PropertyChanged(这是新的全局::System.ComponentModel.PropertyChangedEventArgs(属性));
}
}
}

谢谢

客户对象需要看起来像这样

[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; }
}
签出此链接

此处已应答


谢谢大家。

粘贴您的客户对象。听起来您缺少DataContract和DataMember属性。添加服务引用时,客户类(在客户端)由代理自动生成。在服务器上,Customer是从entity Framework生成的实体。您能在顶部看到DataContract属性吗?如果不是,你可能需要一个伙伴类。很抱歉,错过了您正在使用生成的客户端的事实。您不应该使用它来查询方法吗?它将自动调用+反序列化,而不是您需要使用HttpGetJust更新我的帖子,并使用服务引用创建的代理“Customer”。但是,我对客户类没有任何问题。我在Uri中使用“Customers”来获取“Customers”列表,一切都正常(即使是CRUD)。此错误仅适用于服务操作,而且我正在使用传统的“WebRequest”方式执行此错误。不需要设置name属性。只需使用S创建的代理“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; }
}