I need to convert an IEnumerable object to XML string

IEnumerable<KeyValuePair<string, object>>  myObject 

I tried using the below code ,but this is not working

ConvertToXmlString(myObject );

public static string ConvertToXmlString(object name)
        {
            try
            {
                var stringwriter = new System.IO.StringWriter();
                var serializer = new XmlSerializer(name.GetType());
                serializer.Serialize(stringwriter, name);
                return stringwriter.ToString();
            }
            catch
            {
                return "cannot be converted to XML";
            }
        }

Any suggestions ?

shareimprove this question
   
btw, name is a very bad parameter name is your case – Artur Udod Jun 6 '13 at 21:29
   
and why dont you just use Dictionary – Artur Udod Jun 6 '13 at 21:31
IEnumerable<KeyValuePair<string, object>>  myObject = ... 
XElement el = new XElement("root",
    myObject.Select(kv => new XElement(kv.Key, kv.Value)));
el.Save("myFile.xml"); // if you need to save it to file
shareimprove this answer