Tuesday, July 19, 2011

SerializerDictionary / DerializeDictionary

public static string SerializerDictionary(Dictionary<stringstring> serializeObject)
        {
            var xmlDoc = new XmlDocument();
            var xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, """");
            xmlDoc.AppendChild(xmlNode);
            var xmlElements = xmlDoc.CreateElement("""Elements""");
            xmlDoc.AppendChild(xmlElements);
 
            foreach (var element in serializeObject)
            {
                var xmlElement = xmlDoc.CreateElement("""Element""");
                xmlElements.AppendChild(xmlElement);
 
                var xmlAttribute = xmlDoc.CreateAttribute(element.Key);
                xmlAttribute.Value = element.Value;
                xmlElement.Attributes.Append(xmlAttribute);
            }
 
            return xmlDoc.InnerXml;
        }
 
        public static Dictionary<stringstring> DerializeDictionary(string deserializeObject)
        {
            var deserializedDictionary = new Dictionary<stringstring>();
            var xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(deserializeObject);
            foreach (XmlElement element in xmlDoc.GetElementsByTagName("Element"))
            {
                deserializedDictionary.Add(element.Attributes[0].Name, element.Attributes[0].Value);
            }
            return null;
        }

No comments:

Post a Comment