I am wondering why the XmlDictionary.Add()
method returns XmlDictionaryString
. In what scenario would this object be useful?
From MSDN:
If the string value is already in the dictionary, the XmlDictionaryString previously created for it is returned.
This could be useful if you didn't want to have to check if the item is in the dictionary before adding it, you'd just be able to use the return value.
An XmlDictionary
is intended for use in compressing XML:
Dictionaries establish a mapping between commonly appearing text strings, and integers, and provide an effective mechanism for compressing and decompressing XML
So, for each string added to the dictionary, an integer is associated with it. One of the most common use cases after adding a string to the dictionary is to want to use the integer that is now associated with that string.
And the XmlDictionaryString
's Key
property provides access to that integer.
XML Dictionary is used to store the mapping between commonly used string & integer values in the xml and provides a way to compress/decompressing the XML.
This method is used add a string value as Dictionary entry.This method returns an instance of System.Xml.XmlDictionaryString
object which represents a dictionary entry
.This object exposes the properties Key & Value
which returns the dictionary entry’s key & value.
XmlDictionary dict = new XmlDictionary();
XmlDictionaryString dictEntry = dict.Add("Name");
Console.WriteLine("Key:{0},Value:{1}",dictEntry.Key , dictEntry.Value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With