如何重命名序列化对象列表后生成的 XML属性

时间:2021-01-26 10:48:53

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?

我正在序列化对象列表 , XmlSerializer生成 属性,我想重命名它或删除它。可以通过创建将列表封装为字段的新类来完成吗?

 [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>

3 个解决方案

#1


25  

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

最可靠的方法是声明最外层的DTO类:

[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.

这样做的问题是,当您使用自定义属性时,您需要非常小心地存储和重用序列化器,否则会将大量动态程序集加载到内存中。如果您仅仅使用XmlSerializer(类型)构造函数,就可以避免这种情况,因为它会自动地在内部缓存这个构造函数。

#2


4  

Change the following line from:

将以下一行从:

XmlSerializer serializer = new XmlSerializer(typeof(List<TestObject>));

To:

:

XmlRootAttribute root = new XmlRootAttribute("TestObjects");     

XmlSerializer serializer = new XmlSerializer(typeof(List<TestObject>), root);

It should work.

它应该工作。

#3


1  

Create another class like :

创建另一个类,如:

       [XmlRoot("TestObjects")]
       public class TestObjects: List<TestObject>
       {

       }

Then apply below code while sealizing :

然后在分离时应用下面的代码:

            XmlSerializer serializer = new XmlSerializer(typeof(TestObjects));
            MemoryStream memStream = new MemoryStream();
            serializer.Serialize(memStream, tmpList);

#1


25  

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

最可靠的方法是声明最外层的DTO类:

[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.

这样做的问题是,当您使用自定义属性时,您需要非常小心地存储和重用序列化器,否则会将大量动态程序集加载到内存中。如果您仅仅使用XmlSerializer(类型)构造函数,就可以避免这种情况,因为它会自动地在内部缓存这个构造函数。

#2


4  

Change the following line from:

将以下一行从:

XmlSerializer serializer = new XmlSerializer(typeof(List<TestObject>));

To:

:

XmlRootAttribute root = new XmlRootAttribute("TestObjects");     

XmlSerializer serializer = new XmlSerializer(typeof(List<TestObject>), root);

It should work.

它应该工作。

#3


1  

Create another class like :

创建另一个类,如:

       [XmlRoot("TestObjects")]
       public class TestObjects: List<TestObject>
       {

       }

Then apply below code while sealizing :

然后在分离时应用下面的代码:

            XmlSerializer serializer = new XmlSerializer(typeof(TestObjects));
            MemoryStream memStream = new MemoryStream();
            serializer.Serialize(memStream, tmpList);