1 void Main()
2 {
3 var myList = new List<XmlSetting>();
4 myList.Add(new XmlSetting("name1", "value1") { IsProtected = false });
5 myList.Add(new XmlSetting("name2", "value2") { IsProtected = true });
6 myList.Add(new XmlSetting("name3", "value3") { IsProtected = false, DisplayName = "Name 3" });
7
8 GetEntityXml<XmlSetting>(myList, "Settings").Dump();
9 }
10
11 public XmlDocument GetEntityXml<T>(IEnumerable<T> list, string rootNode)
12 {
13 XmlDocument xmlDoc = new XmlDocument();
14 XPathNavigator nav = xmlDoc.CreateNavigator();
15 using (XmlWriter writer = nav.AppendChild())
16 {
17 XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute(rootNode));
18 ser.Serialize(writer, list);
19 }
20 return xmlDoc;
21 }
22
23 [XmlRoot("Test")]
24 public class XmlSetting
25 {
26 [XmlAttribute("Name")]
27 public string Name { get; set; }
28
29 [XmlAttribute("Value")]
30 public string Value { get; set; }
31
32 [XmlAttribute("DisplayName")]
33 public string DisplayName { get; set; }
34
35 [XmlAttribute("IsProtected")]
36 public bool IsProtected { get; set; }
37
38 public XmlSetting(string name, string value) {
39 this.Name = name;
40 this.Value = value;
41 }
42
43 public XmlSetting() {}
44 }