c# - Xml serialization Exception : The type UserQuery+SpecificContentItem was not expected. Use the XmlInclude or SoapInclude -


I have a problem on class serializing to XML I have created a sample code that shows an error. The class, which I want to serialize "content container", is a collection of items in the content container, which is the type "ContentItemBase". Because I have implemented these classes according to my needs. But when the code reaches the part of the actual serial call, the serializer throws this exception:

Type User Time + XmlInclude to specify those types of specific content items were not expected Or use the SoapInclude attribute that are not known statically.

I have searched this problem but I can not apply the XmlInclude method described in the exception message. Is there a solution (design or implementation strategy) for this problem and similar problems?

code:

  zero main () {var items = new specific content {name = "test", value = "test value", specific field = "testfield"} ; Var Container = New Content Container (); Container.Items.Add (item); Container .name = "test container"; XmlSerializer ser = New XmlSerializer (Typef (Content Container)); Stringworld author = new stringwriter (); Serialize (author, container); String result = author. Toasting (); } Public abstract class ContentItemBase {public abstract string name {get; Set;} public abstract string value {get; Set;}} public class specific content item: contentimase {public string specification {get; Set;} public override string name {get; Set;} public override string value {get; Set;}} Public Class Content Container {Public Content Container () {items = new contentimteclosure (); } Public string name {get; Set;} Public ContentItemCollection Items {get; Set;}} Public class ContentItemCollection: IEnumerable & lt; ContentItemBase & gt; {Receive public specific content item specific item { Set; } Public IEnumerator & lt; ContentItemBase & gt; GetEnumerator () {yield returns (SpecificItem! = Null) specificItem; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () {Return GetEnumerator (); } Add Public Zero (object obj) {if obj is specific content is itm} specific item = (specific content ITM) obj; Creating your serializer as follows:  
       

should do the trick.

You can add serialize method to ContentContainer class >

  public string string ize () {var type = item. Selection (x = & gt; x.GetType ()). Specific (). ToArray (); XmlSerializer ser = New XmlSerializer (typef (content container), type); Stringworld author = new stringwriter (); Serialize (author, it); Return Author Toasting (); }    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -