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

时间:2010-06-27 21:44:55

标签: c# xml xml-serialization

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

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

4 个答案:

答案 0 :(得分:29)

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

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

并序列化 (即将您的列表放入另一个对象)。


可以避免包装类,但我不会:

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; }
}

问题,当您使用自定义属性时,您需要非常小心地存储和重新使用序列化程序,否则您将获得批次加载到内存中的动态程序集。如果您只使用XmlSerializer(Type)构造函数,则可以避免这种情况,因为它会自动在内部缓存它。

答案 1 :(得分:6)

更改以下行:

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

要:

XmlRootAttribute root = new XmlRootAttribute("TestObjects");     

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

它应该有用。

答案 2 :(得分:2)

您可以向XmlSerializer构造函数添加一个附加参数,以从本质上命名根元素。

XmlSerializer xsSubmit = new XmlSerializer(typeof(List<DropDownOption>), new XmlRootAttribute("DropDownOptions"));

这将导致以下结构:

<DropDownOptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <DropDownOption>
      <ID>1</ID>
      <Description>foo</Description>
    </DropDownOption>
    <DropDownOption>
      <ID>2</ID>
      <Description>bar</Description>
    </DropDownOption>
</DropDownOptions>

答案 3 :(得分:1)

创建另一个类,如:

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

       }

然后在密封时应用以下代码:

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