Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename <ArrayOf> XML attribute that generated after serializing List of objects

I am serializing List of objects List<TestObject> , and XmlSerializer generates <ArrayOfTestObject> attribute, I want rename it or remove it.
Can it be done with creating new class that encapsulated List as field?

 [XmlRoot("Container")]      public class TestObject  {      public TestObject() { }                               public string Str { get; set; }                           }   List<TestObject> tmpList = new List<TestObject>();   TestObject TestObj = new TestObject();  TestObj.Str = "Test";   TestObject TestObj2 = new TestObject();  TestObj2.Str = "xcvxc";   tmpList.Add(TestObj);  tmpList.Add(TestObj2);    XmlWriterSettings settings = new XmlWriterSettings();  settings.OmitXmlDeclaration = true;  settings.Indent = true;  XmlSerializer serializer = new XmlSerializer(typeof(List<TestObject>));   using (XmlWriter writer = XmlWriter.Create(@"C:\test.xml", settings))  {                    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();      namespaces.Add(string.Empty, string.Empty);      serializer.Serialize(writer, tmpList, namespaces);                             }   <ArrayOfTestObject>   <TestObject>     <Str>Test</Str>   </TestObject>   <TestObject>     <Str>xcvxc</Str>   </TestObject> </ArrayOfTestObject> 
like image 267
ilann Avatar asked Jun 27 '10 21:06

ilann


People also ask

How do I serialize an array of XML elements?

You can also serialize an array as a flat sequence of XML elements by applying a XmlElementAttribute to the field returning the array as follows. A serialized instance might resemble the following.

How do I change the name of an XML element?

By applying a XmlArrayAttribute, you can change the name of the XML element, as follows. The resulting XML might resemble the following. The XmlArrayItemAttribute, on the other hand, controls how the items contained in the array are serialized. Note that the attribute is applied to the field returning the array.

How to serialize an array as a flat sequence of elements?

A serialized instance might resemble the following. You can also serialize an array as a flat sequence of XML elements by applying a XmlElementAttribute to the field returning the array as follows. A serialized instance might resemble the following.

What happens when deserializing an XML document?

When deserializing, the array will be filled with XmlAttribute objects that represent all XML attributes unknown to the schema. Public field, property, parameter, or return value that returns an array of XmlElement objects.


1 Answers

The most reliable way is to declare an outermost DTO class:

[XmlRoot("myOuterElement")] public class MyOuterMessage {     [XmlElement("item")]     public List<TestObject> Items {get;set;} } 

and serialize that (i.e. put your list into another object).


You can avoid a wrapper class, but I wouldn't:

class Program {     static void Main()     {         XmlSerializer ser = new XmlSerializer(typeof(List<Foo>),              new XmlRootAttribute("Flibble"));         List<Foo> foos = new List<Foo> {             new Foo {Bar = "abc"},             new Foo {Bar = "def"}         };         ser.Serialize(Console.Out, foos);     } }  public class Foo {     public string Bar { get; set; } } 

The problem with this is that when you use custom attributes you need to be very careful to store and re-use the serializer, otherwise you get lots of dynamic assemblies loaded into memory. This is avoided if you just use the XmlSerializer(Type) constructor, as it caches this internally automatically.

like image 134
Marc Gravell Avatar answered Sep 21 '22 17:09

Marc Gravell