Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If it is a direct map, you should just be able to use it directly, as long as the types as public and have public parameterless constructors, and the properties (including lists) are get/set.</p> <p>If you need to tweak the names there is an <code>XmlSerializer</code> constructor that allows you to specify all the attributes. This is ideal for your scenario, but you <strong>must</strong> cache and re-use the serializer if you use this constructor overload, otherwise you will leak memory (the dynamic assemblies are not collected).</p> <p>Here's a full example that removes one property (<code>XmlIgnore</code>), changes another to an attribute, and leaves a third as an element.</p> <pre><code>using System; using System.IO; using System.Xml.Serialization; public class Foo { public int A { get; set; } public string B { get; set; } public int C { get; set; } } static class Program { static readonly XmlSerializer serializer; static Program() { XmlAttributeOverrides or = new XmlAttributeOverrides(); or.Add(typeof(Foo), "A", new XmlAttributes { // change to an attrib XmlAttribute = new XmlAttributeAttribute("tweaked") }); or.Add(typeof(Foo), "B", new XmlAttributes { XmlIgnore = true // turn this one off }); // leave C as a default named element serializer = new XmlSerializer(typeof(Foo), or); } static void Main() { Foo foo = new Foo { A = 123, B = "def", C = 456 }, clone; string xml; using (StringWriter sw = new StringWriter()) { serializer.Serialize(sw, foo); xml = sw.ToString(); } using (StringReader sr = new StringReader(xml)) { clone = (Foo)serializer.Deserialize(sr); } Console.WriteLine(xml); Console.WriteLine(); Console.WriteLine(clone.A); Console.WriteLine(clone.B); Console.WriteLine(clone.C); } } </code></pre> <p>Note also that if you only need to change things at the <em>type</em> level (such as <code>[XmlInclude]</code>) then you can do this via the <code>partial class</code> that LINQ-to-SQL generates; for example:</p> <pre><code>namespace My.Dal.Namespace { // add a type attribute to SomeEntity [XmlInclude(typeof(SomeDerivedEntity))] partial class SomeEntity { } } </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