From 97ebdd4607a3d6aa312adb07292b13ae2b120929 Mon Sep 17 00:00:00 2001 From: Melanie Date: Sat, 19 Sep 2009 16:57:15 +0100 Subject: Adding Xml serialization of Dictionary where object is either another Dictionary or a value that is convertible to a string. --- OpenSim/Server/Base/ServerUtils.cs | 72 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'OpenSim/Server') diff --git a/OpenSim/Server/Base/ServerUtils.cs b/OpenSim/Server/Base/ServerUtils.cs index 0a36bbe..ae7ec0f 100644 --- a/OpenSim/Server/Base/ServerUtils.cs +++ b/OpenSim/Server/Base/ServerUtils.cs @@ -183,5 +183,77 @@ namespace OpenSim.Server.Base return result; } + + public static string BuildQueryString(Dictionary data) + { + string qstring = String.Empty; + + foreach(KeyValuePair kvp in data) + { + string part; + if (kvp.Value != String.Empty) + { + part = System.Web.HttpUtility.UrlEncode(kvp.Key) + + "=" + System.Web.HttpUtility.UrlEncode(kvp.Value); + } + else + { + part = System.Web.HttpUtility.UrlEncode(kvp.Key); + } + + if (qstring != String.Empty) + qstring += "&"; + + qstring += part; + } + + return qstring; + } + + public static string BuildXmlResponse(Dictionary data) + { + XmlDocument doc = new XmlDocument(); + + XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, + "", ""); + + doc.AppendChild(xmlnode); + + XmlElement rootElement = doc.CreateElement("", "ServerResponse", + ""); + + doc.AppendChild(rootElement); + + BuildXmlData(rootElement, data); + + return doc.InnerXml; + } + + private static void BuildXmlData(XmlElement parent, Dictionary data) + { + foreach (KeyValuePair kvp in data) + { + XmlElement elem = parent.OwnerDocument.CreateElement("", + kvp.Key, ""); + + if (kvp.Value is Dictionary) + { + XmlAttribute type = parent.OwnerDocument.CreateAttribute("", + "type", ""); + type.Value = "List"; + + elem.Attributes.Append(type); + + BuildXmlData(elem, (Dictionary)kvp.Value); + } + else + { + elem.AppendChild(parent.OwnerDocument.CreateTextNode( + kvp.Value.ToString())); + } + + parent.AppendChild(elem); + } + } } } -- cgit v1.1