XML : How to serialize XML without writing the type as a tag

I am trying to write out an XML file, by serializing my class. The class includes a dictionary<string,object> called OptionList, and I have used some code I found here on Stack Overflow to create a serializable dictionary: http://stackoverflow.com/a/1728996/3364648

However, it's not quite giving me what I want. After tweaking the code slightly, I'm getting closer. My WriteXml now looks like this:

      public void WriteXml(System.Xml.XmlWriter writer)      {          XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));          XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));            foreach (TKey key in this.Keys)          {              writer.WriteStartElement("Option");                keySerializer.Serialize(writer, key);                writer.WriteStartElement("value");              TValue value = this[key];              valueSerializer.Serialize(writer, value);              writer.WriteEndElement();                writer.WriteEndElement();          }      }    

and outputs this:

    <OptionList>      <Option>        <string>UNCOMMITTED</string>        <value>          <anyType xsi:type="xsd:boolean">true</anyType>        </value>      </Option>      <Option>        <string>DEADLOCK</string>        <value>          <anyType xsi:type="xsd:boolean">true</anyType>        </value>      </Option>    </OptionList>    

What I want as output is this:

    <OptionList>      <Option>        <key>UNCOMMITTED</key>        <value xsi:type="xsd:boolean">true</value>      </Option>      <Option>        <key>DEADLOCK</key>        <value xsi:type="xsd:boolean">true</value>      </Option>    </OptionList>    

Any ideas on how to replace <string> with <key> and <anytype> with <value>?

No comments:

Post a Comment