For you!
Step 1. Convert Your data from DB to Object
Step 2: Declare Object with "Key" and "Value" (Try Dictionary if you want) and push your data to list Object
Step 3: Using String Builder and Json convert
Step 4: Using XmlWriter to convert your data list to XML file
public class KeyValues
{
public string Key { get; set; }
public string Value { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<KeyValues> lst = new List<KeyValues>();
var usr1 = new KeyValues
{
Key = "secName",
Value = "USD 0f10Y Pay Fixed - 27/02/2017",
};
var usr2 = new KeyValues
{
Key = "secTypeCd",
Value = "SWAPIRS",
};
var usr3 = new KeyValues
{
Key = "swapTenor",
Value = "10Y",
};
lst.Add(usr1);
lst.Add(usr2);
lst.Add(usr3);
string strserialize = JsonConvert.SerializeObject(lst);
JArray jArray = JArray.Parse(strserialize);
StringBuilder finalResult = new StringBuilder("{");
foreach (JObject keys in jArray)
{
finalResult.Append("'" + keys["Key"] + "'" + ":" + "'" + keys["Value"] + "'" + ",");
}
finalResult.Remove(finalResult.Length - 1, 1);
finalResult.Append("}");
JObject finalResultToArr = JObject.Parse(finalResult.ToString());
Console.WriteLine(finalResultToArr);
Dictionary<string, string> dictObj = finalResultToArr.ToObject<Dictionary<string, string>>();
var lstKey = dictObj.Keys;
using (XmlWriter writer = XmlWriter.Create("C:/Data/develop.xml"))
{
writer.WriteStartElement("develop");
foreach (var key in lstKey)
{
writer.WriteElementString(key.ToString(), finalResultToArr[key].ToString());
}
writer.WriteEndElement();
writer.Flush();
}
}
}