Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not with just attributes, no. However, this hairy looking code can indeed read your XML properly into a <code>Root</code> class:</p> <pre><code>[XmlRoot("root")] public class Root : IXmlSerializable { [XmlElement("creatures")] public Creatures Items { get; set; } /// &lt;summary&gt; /// This method is reserved and should not be used. When implementing /// the IXmlSerializable interface, you should return null (Nothing in /// Visual Basic) from this method, and instead, if specifying a custom /// schema is required, apply the /// &lt;see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"/&gt; /// to the class. /// &lt;/summary&gt; /// &lt;returns&gt; /// An &lt;see cref="T:System.Xml.Schema.XmlSchema"/&gt; that describes the /// XML representation of the object that is produced by the /// &lt;see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"/&gt; /// method and consumed by the /// &lt;see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"/&gt; /// method. /// &lt;/returns&gt; public XmlSchema GetSchema() { return null; } /// &lt;summary&gt; /// Generates an object from its XML representation. /// &lt;/summary&gt; /// &lt;param name="reader"&gt;The &lt;see cref="T:System.Xml.XmlReader"/&gt; stream /// from which the object is deserialized. &lt;/param&gt; public void ReadXml(XmlReader reader) { reader.ReadStartElement("root"); reader.ReadStartElement("creatures"); List&lt;Creature&gt; creatures = new List&lt;Creature&gt;(); while ((reader.NodeType == XmlNodeType.Element) &amp;&amp; (reader.Name == "creature")) { Creature creature; string type = reader.GetAttribute("type"); string name = reader.GetAttribute("name"); reader.ReadStartElement("creature"); switch (type) { case "mammal": MammalCreature mammalCreature = new MammalCreature(); reader.ReadStartElement("sound"); mammalCreature.Sound = reader.ReadContentAsString(); reader.ReadEndElement(); creature = mammalCreature; break; case "bird": BirdCreature birdCreature = new BirdCreature(); reader.ReadStartElement("color"); birdCreature.Color = reader.ReadContentAsString(); reader.ReadEndElement(); creature = birdCreature; break; default: reader.Skip(); creature = new Creature(); break; } reader.ReadEndElement(); creature.Name = name; creature.Type = type; creatures.Add(creature); } reader.ReadEndElement(); this.Items = new Creatures(); this.Items.Creature = creatures.ToArray(); reader.ReadEndElement(); } /// &lt;summary&gt; /// Converts an object into its XML representation. /// &lt;/summary&gt; /// &lt;param name="writer"&gt;The &lt;see cref="T:System.Xml.XmlWriter"/&gt; stream /// to which the object is serialized. &lt;/param&gt; public void WriteXml(XmlWriter writer) { new XmlSerializer(typeof(Creatures)).Serialize(writer, this.Items); } } [XmlRoot("creatures")] public class Creatures { [XmlElement("creature")] public Creature[] Creature { get; set; } } [XmlInclude(typeof(MammalCreature))] [XmlInclude(typeof(BirdCreature))] public class Creature { [XmlAttribute("type")] public string Type { get; set; } [XmlAttribute("name")] public string Name { get; set; } } public class MammalCreature : Creature { [XmlElement("sound")] public string Sound { get; set; } } public class BirdCreature : Creature { [XmlElement("color")] public string Color { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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