Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Say you have a <em>Player</em> class that looks like:</p> <pre><code>[XmlRoot] public class Player { [XmlElement] public int Level { get; set; } [XmlElement] public int Health { get; set; } } </code></pre> <p>Here is a complete round-trip to get you started:</p> <pre><code>XmlSerializer xmls = new XmlSerializer(typeof(Player)); StringWriter sw = new StringWriter(); xmls.Serialize(sw, new Player { Level = 5, Health = 500 }); string xml = sw.ToString(); Player player = xmls.Deserialize(new StringReader(xml)) as Player; </code></pre> <p><em>xml</em> is:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt; &lt;Player xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Level&gt;5&lt;/Level&gt; &lt;Health&gt;500&lt;/Health&gt; &lt;/Player&gt; </code></pre> <p>And you guess <em>player</em> is exactly the same as the original object we serialized.</p> <p>If you want to serialize to/deserialize from files you can do something like:</p> <pre><code>using (var stream = File.OpenWrite("my_player.xml")) { xmls.Serialize(stream, new Player { Level = 5, Health = 500 }); } Player player = null; using (var stream = File.OpenRead("my_player.xml")) { player = xmls.Deserialize(stream) as Player; } </code></pre> <p><strong>EDIT:</strong></p> <p>IF you want exactly the XML you show:</p> <pre><code>XmlSerializer xmls = new XmlSerializer(typeof(Player)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlWriterSettings settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true }; using (var stream = File.OpenWrite("my_player.xml")) { using (var xmlWriter = XmlWriter.Create(stream, settings)) { xmls.Serialize(xmlWriter, new Player { Level = 5, Health = 500 }, ns); } } Player player = null; using (var stream = File.OpenRead("my_player.xml")) { player = xmls.Deserialize(stream) as Player; } </code></pre>
    singulars
    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