Note that there are some explanatory texts on larger screens.

plurals
  1. POXML Serializer: why deserialization doesn't work in my case?
    primarykey
    data
    text
    <p>guys. I use XMLSerializer to keep and restore program options. Here is the code:</p> <pre><code>using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml; using System.Windows.Forms; using System.Xml.Serialization; namespace XMLAsk { class Test { public static string ConfigFileName = "C:\\Work\\TMP\\Config.xml"; public static void Main() { MyOptions myOptions = new MyOptions(); myOptions.Title = "Hello, world!"; myOptions.Rating = 15; SerializeToXML(myOptions); MyOptions myOptions2 = new MyOptions(); DeserializeFromXML(myOptions2); MessageBox.Show(myOptions2.Title); } public static void SerializeToXML(MyOptions myOptions) { XmlSerializer serializer = new XmlSerializer(typeof(MyOptions)); TextWriter textWriter = new StreamWriter(ConfigFileName); serializer.Serialize(textWriter, myOptions); textWriter.Close(); } public static void DeserializeFromXML(MyOptions myOptions2) { XmlSerializer deserializer = new XmlSerializer(typeof(MyOptions)); TextReader textReader = new StreamReader(ConfigFileName); myOptions2 = (MyOptions)deserializer.Deserialize(textReader); textReader.Close(); } } public class MyOptions { private string title; private int rating; public string Title { get { return title; } set { title = value; } } public int Rating { get { return rating; } set { rating = value;} } } } </code></pre> <p>Serialization does work. I get the following xml-file (Config.xml):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; - &lt;MyOptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Title&gt;Hello, world!&lt;/Title&gt; &lt;Rating&gt;15&lt;/Rating&gt; &lt;/MyOptions&gt; </code></pre> <p>But deserialization seems doesn't work. For instance, when I display one of options with <strong>MessageBox.Show(myOptions2.Title);</strong> (see code above), then I get an empty string.</p> <p>Why? What's wrong with my code?</p>
    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. 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