Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Like so?</p> <pre><code>using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml.Serialization; public class MyBase { } public class ConcreteType : MyBase { [XmlAttribute("att")] public string Value { get; set; } } [XmlRoot("mycoreobject")] public class SomeRoot { public List&lt;MyBase&gt; Items { get; set; } } static class Program { static void Main() { XmlAttributeOverrides xao = new XmlAttributeOverrides(); XmlAttributes extraTypes = new XmlAttributes(); extraTypes.XmlArray = new XmlArrayAttribute("somethings"); // assume loaded dynamically, along with name/namespace extraTypes.XmlArrayItems.Add(new XmlArrayItemAttribute("plugintype1", typeof(ConcreteType)) { Namespace = "my-plugin-namespace" }); xao.Add(typeof(SomeRoot), "Items", extraTypes); // important: need to cache and re-use "ser", or will leak assemblies XmlSerializer ser = new XmlSerializer(typeof(SomeRoot), xao); //example 1: writing var obj = new SomeRoot { Items = new List&lt;MyBase&gt; { new ConcreteType { Value = "abc" } } }; XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); ns.Add("ass1", "my-plugin-namespace"); ser.Serialize(Console.Out, obj, ns); // example 2: reading string xml = @"&lt;mycoreobject xmlns:ass1=""my-plugin-namespace""&gt; &lt;somethings&gt; &lt;ass1:plugintype1 att=""aaa""/&gt; &lt;/somethings&gt; &lt;/mycoreobject&gt;"; obj = (SomeRoot)ser.Deserialize(new StringReader(xml)); var foundIt = (ConcreteType)obj.Items.Single(); Console.WriteLine(); Console.WriteLine(foundIt.Value); // should be "aaa" } } </code></pre>
 

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