Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with serialization of NameKeyValue and List<>
    primarykey
    data
    text
    <p>I'll try to show my problems.</p> <p>I have a list of pairs key-values that I want to serialize to a XML file. I've read others questions (<a href="https://stackoverflow.com/questions/2658916/serializing-a-list-of-key-value-pairs-to-xml">question</a> - <a href="http://www.techrepublic.com/blog/howdoi/how-do-i-serialize-a-hash-table-in-c-when-the-application-requires-it/143" rel="nofollow noreferrer">question</a>) in this site and finally I have done this:</p> <pre><code>[Serializable] [XmlType(TypeName = "Pair")] public class NameValuePair { public String Key { get; set; } public Object Value { get; set; } public NameValuePair() { Key = null; Value = null; } public NameValuePair(String k, Object v) { Key = k; Value = v; } } [Serializable] [XmlType(TypeName = "FileTransfer_Configuration_File")] public class ConfigTable : List&lt;NameValuePair&gt; { //more code } //Serialization example XmlSerializer serializer = new XmlSerializer(typeof(ConfigTable)); using (StreamWriter writer = new StreamWriter(file)) { serializer.Serialize(writer, configtable); } </code></pre> <p>And the result is perfectly OK:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FileTransfer_Configuration_File xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Pair&gt; &lt;Key&gt;ini_Win&lt;/Key&gt; &lt;Value xsi:type="xsd:boolean"&gt;true&lt;/Value&gt; &lt;/Pair&gt; &lt;Pair&gt; &lt;Key&gt;son_Peticion&lt;/Key&gt; &lt;Value xsi:type="xsd:string"&gt;defecto&lt;/Value&gt; &lt;/Pair&gt; &lt;/FileTransfer_Configuration_File&gt; </code></pre> <p>The problem comes when the value attribute of the NameValuePair is a List&lt;> object. List&lt;> objects are serializable, but not inside a NameValuePair:</p> <pre><code>List&lt;Usuario&gt; aux = new List&lt;Usuario&gt;(); aux.Add(new Usuario("pepito")); aux.Add(new Usuario("juanito")); aux.Add(new Usuario("miguelito", true, "c:/miguelito/")); NameValuePair nvp = new NameValuePair("clave", aux); XmlSerializer serializer = new XmlSerializer(typeof(NameValuePair)); using (StreamWriter writer = new StreamWriter(file)) { serializer.Serialize(writer, nvp); } </code></pre> <p>The result: 'System.InvalidOperationException' in System.Xml.dll and this output:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Pair xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;Key&gt;clave&lt;/Key&gt; //abrupt tnd of file </code></pre> <p>How can I do what I want? Thank you</p>
    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. 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