Converting NameValue pair entity to XML I have the following code, to store a web service name value pair response into DB. I am saving it in xml column of DB. Kindly highlight the issues with this approach. Please note that the name value pair (i.e response.details) array is dynamic and i cannot directly map it to individual db columns.

XElement ex = new XElement("ServiceResponse","");
        for (int i = 0; i &lt; response.details.Length; i++)
        {
            string Name = Regex.Replace(response.details[i].name.Trim(),@"[^A-Za-z0-9.]+","");
            ex.Add(new XElement("Details",new XElement(Name, response.details[i].value)));
        }
using (DBDataContext con = new DBDataContext())
        {
            con.Requests.InsertOnSubmit(new Nettium.FraudManagementBase.DataContext.Request
            {
                response = ex
            }
                );
            con.SubmitChanges();
        }


Thanks
You could achieve a little more readability using Linq2Xml like this:

var ex = new XElement("ServiceResponse",
    response.details.Select(detail =&gt; 
        new XElement("Details", 
            new XElement(CleanName(detail.name), detail.value))));


I have moved the line of code that performs some kind of cleaning on the detail's name to a separate method, like so:

private static string CleanName(string name)
{
    return Regex.Replace(name.Trim(), @"[^A-Za-z0-9.]+", "");
}


You could rename this method to communicate better what it does (for me it's not really obvious). 

Two more things:


it's good to follow the C# naming guidelines. In your code, the local variables begin with a capital letter and the public fields/properties of the Request and Detail classes begin with lowercase letters. It should be the other way around.
you could add a using Nettium.FraudManagementBase.DataContext; clause at the top of the file. The data access code will look like this:

using (DBDataContext con = new DBDataContext())
{
    con.Requests.InsertOnSubmit(new Request { response = ex });
    con.SubmitChanges();
}



which is much cleaner IMO.

So, the final code could look like this:

var xml = new XElement("ServiceResponse",
    response.Details.Select(detail =&gt; 
        new XElement("Details", 
            new XElement(CleanName(detail.Name), detail.Value))));

using (DBDataContext con = new DBDataContext())
{
    con.Requests.InsertOnSubmit(new Request { Response = xml });
    con.SubmitChanges();
}