fork(3) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml.Serialization;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. Document d = new Document();
  10.  
  11. d.Placemarks = new List<Placemark>();
  12.  
  13. d.Placemarks.Add(new Placemark("Mark" + d.Placemarks.Count, "What I am..."));
  14. d.Placemarks.Add(new Placemark("Mark" + d.Placemarks.Count, "What I am..."));
  15. d.Placemarks.Add(new Placemark("Mark" + d.Placemarks.Count, "What I am..."));
  16.  
  17. d.Name = "Test.xml";
  18.  
  19. XmlSerializer x = new XmlSerializer(typeof(Document));
  20. x.Serialize(System.Console.Out, d);
  21. }
  22. }
  23.  
  24. public class Document
  25. {
  26. [XmlElement("name")]
  27. public string Name { set; get; }
  28.  
  29. [XmlElement("open")]
  30. public int Open { set; get; }
  31.  
  32. //This will Serialize to a container <Placemarks>...</Placemarks>
  33. public List<Placemark> Placemarks { set; get; }
  34. }
  35.  
  36. public class Placemark
  37. {
  38. public Placemark() { }
  39. public Placemark(string name, string desc)
  40. {
  41. Name = name;
  42. Description = desc;
  43. }
  44.  
  45. [XmlElement("name")]
  46. public string Name { set; get; }
  47.  
  48. [XmlElement("description")]
  49. public string Description { set; get; }
  50. }
Success #stdin #stdout 0.22s 41848KB
stdin
Standard input is empty
stdout
<?xml version="1.0" encoding="utf-8"?>
<Document xmlns:xsi="http://w...content-available-to-author-only...3.org/2001/XMLSchema-instance" xmlns:xsd="http://w...content-available-to-author-only...3.org/2001/XMLSchema">
  <name>Test.xml</name>
  <open>0</open>
  <Placemarks>
    <Placemark>
      <name>Mark0</name>
      <description>What I am...</description>
    </Placemark>
    <Placemark>
      <name>Mark1</name>
      <description>What I am...</description>
    </Placemark>
    <Placemark>
      <name>Mark2</name>
      <description>What I am...</description>
    </Placemark>
  </Placemarks>
</Document>