.net - 将 XML 中的 <ArrayOf> 反序列化为 List<>

标签 .net xml wcf list serialization

我在反序列化 WCF 网络服务的结果时遇到问题。该方法返回 List<RecipeEntity> ,它被序列化为 XML,如下所示。当我尝试反序列化时,我只是得到一个异常,如下所示。看来我无法反序列化 <ArrayOfRecipe>List<RecipeEntity> .注意 RecipeEntity按合约名称映射到 Recipe .

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

我知道我可以包装 List<RecipeEntity>RecipeList class 并返回它,但我宁愿将任何给定类型直接反序列化为 List<>。

异常(exception):

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: 

数据契约:

[DataContract(Name = "Recipe", Namespace = "Groceries.Entities")]
public class RecipeEntity
{
    [DataMember] public int Id;
    [DataMember] public string Name;
    [DataMember] public string Description;
}

实现:

[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();
    }
}

示例 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>

反序列化代码:

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);
}

最佳答案

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

关于.net - 将 XML 中的 <ArrayOf> 反序列化为 List<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5773312/

相关文章:

python - 使用 XML 响应处理 GET 请求

.net - RESTful WCF 服务在 POST 操作中返回 "endpoint not found"错误

WCF REST 与 Windows Phone 通信

c# - 如何打印出树结构?

.net - 如何加快 .NET 程序集加载性能

c# - 调用 TLS 1.0 和 TLS 1.2 Web 服务的 ASP.NET 应用程序

c# - 400 错误请求将 xml 负载发送到 WCF REST 服务

.net - 将依赖注入(inject)与 Http 处理程序一起使用

android - 如何使用SAX PARSER解析android中的html内容

c# - WCF 数据服务身份验证