c# - Serialize a list of objects without the parent tag -


i have list mylistofitems containing 5 items. want create xml items displayed that:

<something>   <somevalue/> </something>  <item>...</item> <item>...</item> <item>...</item> <item>...</item> <item>...</item>  <somethingelse>   <somevalue/> </somethingelse> 

and far have this:

<something>     <somevalue/> </something>  <mylistofitems>   <item>...</item>   <item>...</item>   <item>...</item>   <item>...</item>   <item>...</item> </mylistofitems>  <somethingelse>   <somevalue /> </somethingelse> 

and how it:

[system.xml.serialization.xmlarrayattribute(form=system.xml.schema.xmlschemaform.unqualified, isnullable=true, order=34)] [system.xml.serialization.xmlarrayitemattribute("passengerdetails", isnullable=false)] public list<item> mylistofitems {     {         return this.mylistofitemsfield;     }     set {         this.mylistofitemsfield = value;         this.raisepropertychanged("mylistofitems");     } } 

how can parent mylistofitems tag omitted?

edit: topics mentioned: if change xmlarrayitemattribute xmlelement this

invalidoperationexception: xmlelement, xmltext, , xmlanyelement cannot used in conjunction xmlattribute, xmlanyattribute, xmlarray, or xmlarrayitem.

and when comment out xmlarrayattribute portion this

invalidoperationexception: inconsistent sequencing: if used on 1 of class's members, 'order' property required on particle-like members, please explicitly set 'order' using xmlelement, xmlanyelement or xmlarray custom attribute on class member 'item'.

now how can set order multiple items?

you not need use serialize. instead use xml linq :

using system.linq; using system.text; using system.xml; using system.xml.linq; using system.io; using system.net;   namespace consoleapplication73 {     class program     {         const string filename = @"c:\temp\test.xml";         static void main(string[] args)         {              xdocument doc = xdocument.load(filename);              list<string> items = doc.descendants("item").select(x => (string)x).tolist();          }      }  } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -