Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does XmlDictionary.Add() return an XmlDictionaryString?

Tags:

c#

.net

I am wondering why the XmlDictionary.Add() method returns XmlDictionaryString. In what scenario would this object be useful?

like image 506
Qué Padre Avatar asked Oct 04 '13 13:10

Qué Padre


3 Answers

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.

like image 68
Gray Avatar answered Nov 15 '22 17:11

Gray


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.

like image 44
Damien_The_Unbeliever Avatar answered Nov 15 '22 15:11

Damien_The_Unbeliever


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);
like image 22
Thilina H Avatar answered Nov 15 '22 16:11

Thilina H