मुझे डब्लूसीएफ webservice से परिणाम deserializing परेशानी है। विधि List<RecipeEntity>
लौटाती है, जो नीचे दिखाए गए एक्सएमएल को क्रमबद्ध है। जब मैं deserialize करने की कोशिश करता हूं तो मुझे नीचे दिखाया गया अपवाद मिलता है। ऐसा लगता है कि मैं <ArrayOfRecipe>
से List<RecipeEntity>
को deserialize नहीं कर सकता। ध्यान दें कि RecipeEntity
अनुबंध नाम से Recipe
पर मैप किया गया है।एक्सएमएल में <ArrayOf> की सूची <<
खोज के बाद मैं कई एक्सएमएलएरे और एक्सएमएलएलमेंट गुणों का प्रस्ताव करता हूं, लेकिन जहां तक मैं कह सकता हूं कि वे GetRecipes()
विधि पर यहां लागू नहीं होते हैं। मैंने केवल उन्हें धारावाहिक वर्गों के क्षेत्रों में उपयोग किया है।
मुझे पता है कि मैं List<RecipeEntity>
को RecipeList
कक्षा में लपेट सकता हूं और इसके बजाय इसे वापस कर सकता हूं, लेकिन मैं किसी भी प्रकार के लिए सीधे <> सूची में deserialize करना होगा।
अपवाद:
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();
}
}
उदाहरण एक्सएमएल परिणाम, चित्रण प्रयोजनों के लिए ही।
<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>
Deserialization कोड:
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);
}