Click here to Skip to main content
16,023,047 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I would like to transform JSON string below:
[{
"secTypeCd": "SWAPIRS"
},
{
"swapTenor": "10Y"
}]


To get this XMl construct:

<envelope><sectypecd>SWAPIRS<swaptenor>10Y<effectivedate>2017-02-27<envelope>


What I have tried:

I have tried this:

xml = JsonConvert.DeserializeXmlNode("{\"envelope\":" + JSONstring + "}", "envelope");
Posted
Updated 30-Jun-21 21:37pm

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)
       {
           //Initial Data
           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);

           //Convert List Data Object To JSON
           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;

           //Convert List Data Object to XML File
           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();
           }
       }
   }
 
Share this answer
 
Hey my friend,
Try this one:
static void Main(string[] args)
       {
           string jsonString = @"[{'secTypeCd':'SWAPIRS'}, {'swapTenor':'10Y'}, {'effectivedate':'2017-02-27'}]";

           var wrappedDocument = string.Format("{{ item: {0} }}", jsonString);
           // To convert JSON text contained in string json into an XML node
           var xDocument = JsonConvert.DeserializeXmlNode(wrappedDocument, "envelope");

           // To convert an XML node contained in string xml into a JSON string
           string jsonText = JsonConvert.SerializeXmlNode(xDocument);


           Console.WriteLine(jsonText);
       }
 
Share this answer
 
Comments
Member 15254125 24-Jun-21 7:44am    
Hi Nguyen,

I have tried your way to add tags before serializing the dictionary of data table rows (which contains tag in column 1 and element value in column 2); It throwed the same error.

To explain the problem in full:

I have a datatable whose rows has (XML tags in column 1 and XML Element value in column 2).

My task is to convert the datatable into JSON string with complete hierarchy intact between parent and child nodes. The same JSON string can then be deserialized to well formed XML.
Mr.Duy09 25-Jun-21 5:37am    
You mean like that, my Friend?
Table
|Name|Value|
|secTypeCd|SWAPIRS|
|swapTenor|10Y|
|effectivedate|2017-02-27

You want to convert Json like that:
{
"secTypeCd": "SWAPIRS",
"swapTenor": "10Y",
"effectivedate": "2017-02-27"
}
and the XML?:

<envelope>
<sectypecd>SWAPIRS
<swaptenor>10Y
<effectivedate>2017-02-27

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900