Serialization question

C

Chris Dunaway

I have a serializable Dictionary (adapted from this blog:
http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx).

The code for it is below:


namespace MyNamespace {
[XmlRoot("dictionary")]
public class SerializableDictionary
: Dictionary<string, string>, IXmlSerializable {

public System.Xml.Schema.XmlSchema GetSchema() {
return null;
}

public void ReadXml(System.Xml.XmlReader reader) {

bool wasEmpty = reader.IsEmptyElement;

//read to the first Parameter element
reader.Read();

if (wasEmpty)
return;

while (reader.Name == "Parameter") {
string key = reader["name"];
string value = reader["value"];

this.Add(key, value);

//Read next node
reader.Read();
}
reader.ReadEndElement();
}

public void WriteXml(System.Xml.XmlWriter writer) {
foreach (string key in this.Keys) {
writer.WriteStartElement("Parameter");
writer.WriteAttributeString("name", key);
writer.WriteAttributeString("value", this[key]);
writer.WriteEndElement();
}
}
}

For each item in the dictionary, it writes serializes as follows:

<Parameter name="SomeName" value="Some Value" />

It works fine, but I want to be able to change the name of the start
element (in this case "Parameter") and somehow specify this when the
class is used. I thought of using a custom attribute so that someone
who uses this class could decorate their variable with the attribute
to change the name of the element that is output. Something like this
(air code):

public class SomeClass {
private SerializableDictionary theDictionary;

[MyCustomAttribute("NewElementName")]
public SerializableDictionary TheDictionary {
get { return theDictionary; }
set { theDictionary = value; }
}
}

How can I tell the serializer to use "NewElementName" instead of
"Parameter" when it serializes the items of the dictionary? I have
not been able to get the value of the MyCustomAttribute from within
the SerializableDictionary class. Is it even possible? The
XmlSerializer has an attribute called XmlArrayItemAttribute which
controls the way array items are serialized, I would like to create
something similar here.

Any suggestions/alternatives are appreciated.

Thanks,

Chris
 
P

Pavel Minaev

public class SomeClass {
    private SerializableDictionary theDictionary;

    [MyCustomAttribute("NewElementName")]
    public SerializableDictionary TheDictionary {
        get { return theDictionary; }
        set { theDictionary = value; }
    }

}

How can I tell the serializer to use "NewElementName" instead of
"Parameter" when it serializes the items of the dictionary?  I have
not been able to get the value of the MyCustomAttribute from within
the SerializableDictionary class.  Is it even possible?

It is not possible, because your dictionary class does not have access
to the context in which it is used (property declaration, local
variable declaration, etc).

My advice would be to just pass the element name to the constructor of
your dictionary, save it in a private field within the dictionary, and
use as needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top