c# - .NET XML Serialization error: Invalid or missing value of the choice identifier -


following instructions from:

.net xml serialization error (there error reflecting type)

create object based on xmlchoiceidentifier

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlchoiceidentifierattribute(v=vs.110).aspx

i tried serialize array of objects xmlchoiceidentifier attribute. apparently serializer requires array , observablecollection cause serializer throw , error (first link). need have observablecollection in order keep ui updated. attempted have serialization engine access array whilst ui access observable collection, albiet in resource wasting fashion.

this "solution"

 [xmltype(includeinschema = false)] public enum itemchoicetype {     [xmlenumattribute("item")]     item,     [xmlenumattribute("macro")]     macro }  [serializable()] public class groupanditemscollection {     [xmlelementattribute(isnullable = false)]     [xmlignore]     public itemchoicetype[] itemtypearray;      [xmlattribute(attributename = "name")]     public string group     {         { return m_group; }         set         {             if (m_group == value)                 return;             m_group = value;             //onpropertychanged("group");         }     }      private observablecollection<listitemname> m_items;     private listitemname[] m_itemsarray;      [xmlignore]     public observablecollection<listitemname> items     {                 {             if (m_items != null && itemsarray != null && m_items.except(itemsarray).any())             {             m_items = new observablecollection<listitemname>(itemsarray);             }             return m_items;         }         set         {             m_items = value;             itemsarray = new listitemname[m_items.count];             (int = 0; < m_items.count; i++)             {                 itemsarray[i] = m_items[i];             }         }     }  [xmlchoiceidentifier("itemtypearray")]     [xmlelement(elementname = "item", type = typeof(observablecollection<listitemname>))]     [xmlelement(elementname = "macro", type = typeof(observablecollection<listitemname>))]     public listitemname[] itemsarray     {         { return m_itemsarray; }         set { m_itemsarray = value; }     }       public groupanditemscollection()     {         //to expand nodes in xamdatatree         isexpanded = true;          items = new observablecollection<listitemname>();         items.collectionchanged += items_collectionchanged;     }      void items_collectionchanged(object sender, system.collections.specialized.notifycollectionchangedeventargs e)     {         itemsarray = new listitemname[m_items.count];         (int = 0; < m_items.count; i++)         {             itemsarray[i] = m_items[i];         }     } } 

then in main method, attempted serialize doing following:

using (streamwriter writer = new streamwriter(dlg.filename))                     {                         itemchoicetype[] ic = new itemchoicetype[]                         {                             itemchoicetype.item,                              itemchoicetype.macro                         };                         xmlserializer xml = null;                                  group.itemtypearray = ic;                             xml = new xmlserializer(typeof(groupanditemscollection));                             xml.serialize(writer, group);                            writer.close();                     } 

with solution, following error:

system.invalidoperationexception occurred   hresult=-2146233079   message=there error generating xml document.   source=system.xml   stacktrace:        @ system.xml.serialization.xmlserializer.serialize(xmlwriter xmlwriter, object o, xmlserializernamespaces namespaces, string encodingstyle, string id)        @ system.xml.serialization.xmlserializer.serialize(textwriter textwriter, object o, xmlserializernamespaces namespaces)        @ system.xml.serialization.xmlserializer.serialize(textwriter textwriter, object o)        @ otometrics.icssuite.reports.viewmodel.editreportvm.<initializecommands>b__38(object o) in    innerexception: system.invalidoperationexception        hresult=-2146233079        message=invalid or missing value of choice identifier 'itemtypearray' of type 'itemchoicetype[]'.        source=microsoft.generatedcode        stacktrace:             @ microsoft.xml.serialization.generatedassembly.xmlserializationwritercustomreportlist.write4_groupanditemscollection(string n, string ns, groupanditemscollection o, boolean isnullable, boolean needtype) 

now, if chose neglect try sync array , observable collection, serialization works... code below serializes fine.

[xmltype(includeinschema = false)] public enum itemchoicetype {     [xmlenumattribute("item")]     item,     [xmlenumattribute("macro")]     macro }  [serializable()] public class groupanditemscollection {     [xmlelementattribute(isnullable = false)]     [xmlignore]     public itemchoicetype[] itemtypearray;      [xmlattribute(attributename = "name")]     public string group     {         { return m_group; }         set         {             if (m_group == value)                 return;             m_group = value;             //onpropertychanged("group");         }     }      private listitemname[] m_itemsarray;  [xmlchoiceidentifier("itemtypearray")]     [xmlelement(elementname = "item", type = typeof(observablecollection<listitemname>))]     [xmlelement(elementname = "macro", type = typeof(observablecollection<listitemname>))]     public listitemname[] itemsarray     {         { return m_itemsarray; }         set { m_itemsarray = value; }     }       public groupanditemscollection()     {         isexpanded = true;     } } 

so previous example makes xmlserializer fail this:

message=invalid or missing value of choice identifier 'itemtypearray' of type 'itemchoicetype[]'. 

although there answers correct error message. none of them worked. wish take credit, alas, smart team lead figured out. happening code used xsd2code produce vb schema had xmlchoiceidentifier. happening in our business class when looping through the code, object itemchoicetype outside loop. when validating xml, first node good, , every node after not good. although had itemselementname had value, 1 iteration , rest of xml validation fail error.

we moved the itemschoicetype inside loop when creating xml , viola! hope helps. might not pertain fix in stack overflow, gave same error , if issue same ours. see page.

happy coding :)


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -