Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I dug a little deeper into this fascinating topic and have to say that working with what amounts to an associative array is just a pain in the .NET SOAP implementation. An associative array is represented as an IDictionary (e.g. Hashtable) – but refuses to be serialized (try it!).</p> <p>The below code is the closest I could get – and for some reason (bug?) it does not work on the .NET framework, but does work on Mono.</p> <pre><code>using System; using System.IO; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; using System.Collections.Generic; using System.Text; public class Test { /* * as per http://stackoverflow.com/a/1072815/2348103 */ public class Item { [XmlElement(Form = XmlSchemaForm.Unqualified)] public string key; [XmlElement(Form = XmlSchemaForm.Unqualified)] public string value; } [SoapType(TypeName = "Map", Namespace = "http://xml.apache.org/xml-soap")] public class Map : List&lt;Item&gt; { } public static void Main() { Map map = new Map(); map.Add( new Item { key="foo", value="bar" } ); map.Add( new Item { key="quux", value="barf" } ); XmlTypeMapping mapping = (new SoapReflectionImporter()).ImportTypeMapping( map.GetType() ); XmlSerializer serializer = new XmlSerializer( mapping ); XmlTextWriter writer = new XmlTextWriter( System.Console.Out ); writer.Formatting = Formatting.Indented; writer.WriteStartElement( "root" ); serializer.Serialize( writer, map ); writer.WriteEndElement(); writer.Close(); } } /* // // does not work - but see http://msdn.microsoft.com/en-us/magazine/cc164135.aspx // public class Map : IXmlSerializable { const string NS = "http://xml.apache.org/xml-soap"; public IDictionary dictionary; public Map() { dictionary = new Hashtable(); } public Map(IDictionary dictionary) { this.dictionary = dictionary; } public void WriteXml(XmlWriter w) { w.WriteStartElement("Map", NS); foreach (object key in dictionary.Keys) { object value = dictionary[key]; w.WriteStartElement("item", NS); w.WriteElementString("key", NS, key.ToString()); w.WriteElementString("value", NS, value.ToString()); w.WriteEndElement(); } w.WriteEndElement(); } public void ReadXml(XmlReader r) { r.Read(); // move past container r.ReadStartElement("dictionary"); while (r.NodeType != XmlNodeType.EndElement) { r.ReadStartElement("item", NS); string key = r.ReadElementString("key", NS); string value = r.ReadElementString("value", NS); r.ReadEndElement(); r.MoveToContent(); dictionary.Add(key, value); } } public System.Xml.Schema.XmlSchema GetSchema() { return null; } } */ </code></pre> <p><strong>Sample output from Mono:</strong></p> <pre><code>&lt;q1:Map xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1" d2p1:arrayType="Item[2]" xmlns:d2p1="http://schemas.xmlsoap.org/soap/encoding/" xmlns:q1="http://xml.apache.org/xml-soap"&gt; &lt;Item href="#id2" /&gt; &lt;Item href="#id3" /&gt; &lt;/q1:Map&gt; [...] </code></pre> <p><strong>Sample output from .NET:</strong></p> <pre><code>&lt;q1:Array xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1" q1:arrayType="Item[2]" xmlns:q1="http://schemas.xmlsoap.org/soap/encoding/"&gt; &lt;Item href="#id2" /&gt; &lt;Item href="#id3" /&gt; &lt;/q1:Array&gt; [...] </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. 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