我正在将我的类序列化为XML,其中一个属性的类型为List
public class MyClass { ... public ListProperties { get; set; } ... }
通过序列化此类创建的XML如下所示:
... ... somethinghere somethinghere
现在我的问题.如何更改我的类来实现这样的XML:
... ... somethinghere somethinghere
序列化后.谢谢你的帮助!
试试XmlArrayItemAttribute:
using System; using System.IO; using System.Xml.Serialization; using System.Collections.Generic; public class Program { [XmlArrayItem("Property")] public ListProperties = new List (); 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); } }
的test.xml:
test1 test2 test3