XmlDictionary And XmlDictionaryString Classes to Create WCF Message



It is common to use the following classes to create messages:

Class Namespace
XmlDictionary System.Xml
XmlDictionaryString System.Xml

Let us investigate the purpose of each class one by one.

The XmlDictionary class allows us to create a private pair of Key and Value. This key-value pair can be used to represent:
  1. Element name
  2. Attribute name
  3. XML namespace declaration.

The XmlDictionary class uses the XmlDictionaryString class object to create a key value pair. If you see the following code, we are creating a XmlDictionary object and List of XmlDictionaryString.
 Xml Dictionary

dct is object of XmlDictionary and while adding string value in this it returns XmlDictionaryString object.

XmlDictionary dct = new XmlDictionary();
            List<XmlDictionaryString> lstData = new List<XmlDictionaryString>();
            lstData.Add(dct.Add("Bat"));
            lstData.Add(dct.Add("Ball"));
            lstData.Add(dct.Add("Wicket"));
            foreach (var r in lstData)
            {
                Console.WriteLine("Key  = {0}  and Value = {1}", r.Key, r.Value);
            }
            Console.ReadKey(true);

If you run above code you will get output as below:

Xml Dictionary

We are simply adding returned XmlDictionaryString object to list of XmlDictionaryString.

Up Next
    Ebook Download
    View all
    Learn
    View all