如何重命名XML序列化列表<string>中使用的子XML元素?

GrZ*_*eCh 2 .net c# xml serialization

我正在将我的类序列化为XML,其中一个属性的类型为List <string>.

public class MyClass {
    ...
    public List<string> Properties { get; set; }
    ...
}
Run Code Online (Sandbox Code Playgroud)

通过序列化此类创建的XML如下所示:

<MyClass>
    ...
    <Properties>
        <string>somethinghere</string>
        <string>somethinghere</string>
    </Properties>
    ...
</MyClass>
Run Code Online (Sandbox Code Playgroud)

现在我的问题.如何更改我的类来实现这样的XML:

<MyClass>
    ...
    <Properties>
        <Property>somethinghere</Property>
        <Property>somethinghere</Property>
    </Properties>
    ...
</MyClass>
Run Code Online (Sandbox Code Playgroud)

序列化后.谢谢你的帮助!

use*_*116 7

试试XmlArrayItemAttribute:

using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;

public class Program
{
    [XmlArrayItem("Property")]
    public List<string> Properties = new List<string>();

    public static void Main(string[] args)
    {
        Program program = new Program();
        program.Properties.Add("test1");
        program.Properties.Add("test2");
        program.Properties.Add("test3");

        XmlSerializer xser = new XmlSerializer(typeof(Program));
        xser.Serialize(new FileStream("test.xml", FileMode.Create), program);
    }
}
Run Code Online (Sandbox Code Playgroud)

的test.xml:

<?xml version="1.0"?>
<Program xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Properties>
    <Property>test1</Property>
    <Property>test2</Property>
    <Property>test3</Property>
  </Properties>
</Program>
Run Code Online (Sandbox Code Playgroud)