How to Serialize a List <T>?

I have class A. Class B and class C are part of class A.

Class A { //Few Properties of Class A List<typeof(B)> list1 = new List<typeof(B)>() List<typeof(C)> list2 = new List<typeof(C)>() Nsystem NotSystem { get; set; } // Enum Property Type } public enum Nsystem { A = 0, B = 1, C = 2 } 

I want to Serialize class A and want to create XML with it; I also want to serialize list1 and list2 as well as Enum ...

What is a good approach to Serialize this XML, because I need the functionality to convert an object to XML and XML to Object ...

What are some good opportunities for this? Thanks

+8
list c # xml serialization
source share
6 answers

You can use XMLSerializer :

 var aSerializer = new XmlSerializer(typeof(A)); StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); aSerializer.Serialize(sw, new A()); // pass an instance of A string xmlResult = sw.GetStringBuilder().ToString(); 

For proper operation, you will also need xml annotations on your types to make sure they are serialized with naming rights, i.e.:

 public enum NSystem { A = 0, B = 1, C = 2 } [Serializable] [XmlRoot(ElementName = "A")] Class A { //Few Properties of Class A [XmlArrayItem("ListOfB")] List<B> list1; [XmlArrayItem("ListOfC")] List<C> list2; NSystem NotSystem { get; set; } } 

Edit:

Enum properties are serialized by default with the name of the property as containing the XML element and its enumeration value as the XML value, i.e. if the NotSystem property in your example is set to C , it will be serialized as

 <NotSystem>C</NotSystem> 

Of course, you can always change the way property is serialized by properly annotating using [XmlAttribute] , so it is serialized as an attribute or [XmlElement("Foobar")] , so it is serialized using Foobar as the name of the element. More documentation is available on MSDN, check the link above.

+8
source share

you can use this

  [XmlArray("array_name")] [XmlArrayItem("Item_in_array")] public List<T> _List; 
0
source share

Do you need to use XML as a view? You can also consider binary representations, which are usually faster and more compact. You have a BinaryFormatter that is built-in or even better to use protocol buffers that are fast and easy to compact. If you need XML, you might consider whether you want to maintain some compatibility with other languages ​​and technologies, or even with platforms. If it is only C # in C #, then both the XmlSerializer for Xml or BinaryFormatter are fine. If you intend to interact with Javascript, you can use JSON (you can try JSON.NET . Also, if you want to support Windows Phone or other more limited devices, it may be easier to use XmlSerializer that works anywhere.

Finally, you may prefer to simply have a common infrastructure and support many mechanisms and transposition of serialization. Microsoft offers a wonderful (albeit slightly slow) solution in WCF . Perhaps if you say more about your system requirements, it will be easier to suggest an implementation. Good thing you have a lot of options.

0
source share

As you can imagine, the cloud can use binary representations. When searching for solutions, I came across this link. Basically, you mark your class A as [Serializable], and the same goes for your classes B and C. Then you can use functions like these for Serialize and Deserialize

0
source share
  public IList<Object> Deserialize(string a_fileName) { XmlSerializer deserializer = new XmlSerializer(typeof(List<Object>)); TextReader reader = new StreamReader(a_fileName); object obj = deserializer.Deserialize(reader); reader.Close(); return (List<Object>)obj; } public void Serialization(IList<Object> a_stations,string a_fileName) { XmlSerializer serializer = new XmlSerializer(typeof(List<Object>)); using (var stream = File.OpenWrite(a_fileName)) { serializer.Serialize(stream, a_stations); } } 
0
source share

JAXB is the best I've ever used.

http://www.oracle.com/technetwork/articles/javase/index-140168.html

From XML:

 QuestionEntity obj = null; try { JAXBContext ctx = JAXBContext.newInstance(QuestionEntity.class); Unmarshaller unmarshaller = ctx.createUnmarshaller(); obj = (QuestionEntity) unmarshaller.unmarshal(new StreamSource(new StringReader(xml))); } catch (JAXBException e) { e.printStackTrace(); } 

In XML:

 JAXBContext ctx = JAXBContext.newInstance(TestEntity.class); Marshaller marshaller = ctx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); StringWriter stringWriter = new StringWriter(); marshaller.marshal(this, stringWriter); return stringWriter.toString(); 
-one
source share

All Articles