Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here some code (one property missed out)</p> <pre><code>using System; using System.Xml; using System.Xml.Serialization; using System.Collections.Generic; [Serializable] [XmlRoot("Product")] public class Product { [XmlElement("Cycle")] public Cycle Cycle { get; set; } [XmlElement("Updates")] public Updates Updates { get; set; } public Product() { Cycle = new Cycle(); Updates = new Updates(); } } [Serializable] public class Cycle { [XmlAttribute("Type")] public string Type { get; set; } } [Serializable] public class Updates { [XmlElement("Item")] public List&lt;Item&gt; Items { get; set; } public Updates() { Items = new List&lt;Item&gt;(); } } [Serializable] public class Item { [XmlElement("Artifact", typeof(Artifact))] public List&lt;Artifact&gt; Artifacts { get; set; } public Item() { Artifacts = new List&lt;Artifact&gt;(); } } [Serializable] public class Artifact { [XmlAttribute("Kind")] public int Kind { get; set; } } </code></pre> <p>Here the code for serialization</p> <pre><code>using System; using System.Xml; using System.Xml.Serialization; using System.Collections.Generic; using System.IO; using System.Text; private static string SerializeMe(object o) { XmlSerializer s = new XmlSerializer(o.GetType()); MemoryStream stream = new MemoryStream(); XmlWriter writer = new XmlTextWriter(stream, Encoding.Default); s.Serialize(writer, o); stream.Flush(); stream.Seek(0, SeekOrigin.Begin); return Encoding.Default.GetString(stream.ToArray()); } </code></pre> <p>Now here the test code on a console</p> <pre><code> static void Main(string[] args) { Product p = new Product(); p.Cycle.Type = "whatever"; Item i = new Item(); i.Artifacts.Add(new Artifact{Kind = 45}); p.Updates.Items.Add(i); Console.WriteLine(SerializeMe(p)); Console.ReadLine(); } </code></pre> <p>Hope this helps :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload