0

I've been scouring the web at MSDN and Google, for an answer to the following question.

How do I serialize a collection that is named as follows in c#?

 <foocollection>
 <fooitem1></fooitem1>
 <fooitem2></fooitem2>
 ...
 </foocollection>

I saw that you can do this in DataContract:

[CollectionDataContract(Name="foocollection", ItemName = "fooitem")]
public class FooCollection<T> : List<T> {
    public FooCollection(){}
    public FooCollection(T[] items){ 
      foreach(var i in items){
        Add(i);
      }
    }
}

The serializer is the default XML serializer from ASP.NET Web API. This code assumes that the XML posted above is coming from the client.

I have successfully serialized the above as dynamic, but dynamic isn't going to be an acceptable solution.

How would I accomplish serialization successfully using any of the below namespaces, or others, with the caveat that I can serialize to the above class? I'm willing to add extra classes as necessary to make the above work just as long as I don't have to make a class for every item in the collection.

 System.ComponentModel.DataAnnotations
 System.Xml.Serialization

Thank you very much in advance.

cr1pto
  • 539
  • 3
  • 13
  • Using Standard XML Serialization you need a class object for each "fooitem". Using custom XElement/XmlElement you can easily create the array from an object. – jdweng Feb 04 '19 at 18:08
  • So you're saying that it's impossible to serialize a collection without creating a class for each item in the collection? – cr1pto Feb 04 '19 at 18:17
  • Is it a property or a or a class? You can create a custom serializer, but it is probably easier to just use XElement library. – jdweng Feb 04 '19 at 18:23
  • It is a class - the output from the class above should be the XML above. I'll spend some time looking at the XML serialization attributes and see if I can work with those. – cr1pto Feb 04 '19 at 18:26
  • You created a method and need and array/list object to go along with the method. Serialization ignores methods. – jdweng Feb 04 '19 at 18:32
  • Related or duplicate: [How do you deserialize XML with dynamic element names?](https://stackoverflow.com/q/37255149/3744182), [How to serialize an array to XML with dynamic tag names](https://stackoverflow.com/q/50415653/3744182), [How to deserialize xml elements that have different names, but the same set of attributes to a typed array/collection](https://stackoverflow.com/q/30910834/3744182), [How to deserialize a numbered array of XML elements](https://stackoverflow.com/q/20677089/3744182). Do any or all of those work for you? – dbc Feb 04 '19 at 23:16

1 Answers1

0

Since You didn't mention what method of xml serialization you're using, it's hard to tell, but you probably should be using DataContractSerializer in order to enable DataContract attributes. To utilize CollectionDataContractAttribute you also should have collection class that you put said attribute to, like in this example

Brat Wiekszy
  • 31
  • 1
  • 4
  • I just updated my question - could you please elaborate on what you are referring to, potentially with a code sample? I've already looked at the docs at MSDN and am still stuck. – cr1pto Feb 04 '19 at 18:24
  • "It isn't working" is rather a vague description of what happens. What is serialized to xml file? Also, what code do you use to serialize such collection? I meant that instance of DataContractSerializer should be created and then have method (ie. WriteObject) invoked on with writer and instance of your collection. Code example is at the beginning of first link tho. You may also need to enable option "Generate serialization assembly" for assembly that contains your collection class. This option is in build options of project properties, iirc. – Brat Wiekszy Feb 04 '19 at 18:41
  • Thank you for asking for clarification. This is using the default serializer for XML by ASP.NET Web API. We are working with data coming from the client. The client is POSTing this data to us. I will look into WriteObject and see what I can do there. – cr1pto Feb 04 '19 at 18:45
  • Well, I'm afraid I can't suggest anything... except taking a closer look into incoming xml. If it has this precise tag naming, namely item index is included withing tag name, then i believe it's wrong. Serializer, when writing xml, would name all item tags to "fooitem". If list is in fact empty, then i'd check this out. – Brat Wiekszy Feb 04 '19 at 19:40