Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 反序列化<;阵列式>;在XML中列出<&燃气轮机;_.net_Xml_Wcf_List_Serialization_C# - Fatal编程技术网

.net 反序列化<;阵列式>;在XML中列出<&燃气轮机;

.net 反序列化<;阵列式>;在XML中列出<&燃气轮机;,.net,xml,wcf,list,serialization,c#,.net,Xml,Wcf,List,Serialization,C#,我无法从我的WCF Web服务反序列化结果。该方法返回一个列表,该列表被序列化为XML,如下所示。当我尝试反序列化时,我只得到一个异常,如下所示。似乎我无法将反序列化为列表。请注意,RecipeEntity通过合同名称映射到Recipe 搜索之后,我看到许多propose XmlArray和XmlElement属性,但据我所知,它们不适用于GetRecipes()方法。我只看到它们用于序列化类的字段 我知道我可以将列表包装在RecipeList类中并返回它,但我更愿意直接反序列化到任何给定类型的

我无法从我的WCF Web服务反序列化结果。该方法返回一个
列表
,该列表被序列化为XML,如下所示。当我尝试反序列化时,我只得到一个异常,如下所示。似乎我无法将
反序列化为
列表
。请注意,
RecipeEntity
通过合同名称映射到
Recipe

搜索之后,我看到许多propose XmlArray和XmlElement属性,但据我所知,它们不适用于
GetRecipes()
方法。我只看到它们用于序列化类的字段

我知道我可以将
列表
包装在
RecipeList
类中并返回它,但我更愿意直接反序列化到任何给定类型的列表

例外情况:

System.InvalidOperationException was caught
  Message=There is an error in XML document (1, 2).
  StackTrace:
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, Object events)
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
       at GroceriesAppSL.Pages.Home.GetRecipesCallback(RestResponse response)
  InnerException: System.InvalidOperationException
       Message=<ArrayOfRecipe xmlns='Groceries.Entities'> was not expected.
       StackTrace:
            at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read5_Recipe()
       InnerException: 
实施:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "Recipes/{username}")]
    List<RecipeEntity> GetRecipes(string username);
}

public class MyService : IMyService
{
    public List<RecipeEntity> GetRecipes(string username)
    {
        return _recipeDB.Recipes.Select(ToEntity).ToList();
    }
}
[服务合同]
公共接口IMyService
{
[经营合同]
[WebGet(ResponseFormat=WebMessageFormat.Xml,UriTemplate=“Recipes/{username}”)]
列出GetRecipes(字符串用户名);
}
公共类MyService:IMyService
{
公共列表GetRecipes(字符串用户名)
{
return _recipeDB.Recipes.Select(ToEntity.ToList();
}
}
示例XML结果,仅供说明

<ArrayOfRecipe xmlns="Groceries.Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Recipe>
<Id>139</Id>
<Name>ExampleRecipe</Name>
<Description>5 L milk;4 eggs</Description>
</Recipe>
<Recipe>...</Recipe>
<Recipe>...</Recipe>
<Recipe>...</Recipe>
...
</ArrayOfRecipe>

139
配方示例
5升牛奶;4个鸡蛋
...
...
...
...
反序列化代码:

using (var xmlReader = XmlReader.Create(new StringReader(response.Content)))
{
    var xs = new System.Xml.Serialization.XmlSerializer(typeof(List<RecipeEntity>));
    var recipes = (List<RecipeEntity>)xs.Deserialize(xmlReader);
}
使用(var xmlReader=xmlReader.Create(newstringreader(response.Content)))
{
var xs=new System.Xml.Serialization.XmlSerializer(typeof(List));
var recipes=(列表)xs.反序列化(xmlReader);
}

您正在使用
DataContractSerializer
进行序列化,并使用
XmlSerializer
进行反序列化。这两种方法并不相同。您必须在反序列化方法中使用
DataContractSerializer
,或者必须使用
XmlSerializerFormat
属性标记您的操作或服务(在这种情况下,WCF将使用
XmlSerializer
而不是
DataContractSerializer
DataContract
DataMember
属性仅适用于
DataContractSerializer
XmlSerializer
使用在
System.Xml.Serialization
命名空间中定义的自己的属性。

只需首先获取响应流,然后使用DataContractSerializer对其进行反序列化

反序列化代码:

using (var xmlReader = XmlReader.Create(new StringReader(response.Content)))
{
    var xs = new System.Xml.Serialization.XmlSerializer(typeof(List<RecipeEntity>));
    var recipes = (List<RecipeEntity>)xs.Deserialize(xmlReader);
}
using(Stream answer=webResponse.GetResponseStream())
    {
     DataContractSerializer xmlSer = new DataContractSerializer(typeof(RecipeEntity[]));
     var RecipeList = (RecipeEntity[])xmlSer.ReadObject(answer);
    }