0

This may be very silly question, but i m not able to find any thing to solve this.

{"Items": [{"id":1},{"id":2}]}

Should be converted to

<items><element><id>1</id></element><element><id>2</id></element></items>

Basically i need element tag to each of the array item. Please use this to understand better. (https://www.freeformatter.com/json-to-xml-converter.html)

I want this to achieve this in C# dot net. If any third party has better performance then that is also acceptable but it should be free like nuget and all.

Important is to add a custom tag to each item of array, each item should be wrapped with a custom tag. Please check the link given in the question and check the field "Element name of JSON array entries:"

Prince
  • 103
  • 9
  • JsonConvert.DeserializeXNode. Use Newtonsoft – Ramankingdom Sep 01 '17 at 05:30
  • Possible duplicate of [How to convert JSON to XML or XML to JSON?](https://stackoverflow.com/questions/814001/how-to-convert-json-to-xml-or-xml-to-json) – Shakir Ahamed Sep 01 '17 at 05:35
  • https://www.newtonsoft.com/json/help/html/ConvertJsonToXml.htm – Ramankingdom Sep 01 '17 at 05:36
  • check these links https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Converters_XmlNodeConverter.htm https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm – Znaneswar Sep 01 '17 at 05:38
  • I want to add a tag(costume tag) to each element. Please go to the link given in the question, and see there you can give any name to the box named :"Element name of JSON array entries:" – Prince Sep 01 '17 at 05:48

3 Answers3

0

you can use Newtonsoft.Json (Nuget package) for that..

Please refer below link for more info. http://www.c-sharpcorner.com/blogs/how-to-convert-json-into-xml-or-xml-into-json

0

If you have this string in C# directly, and you are not using a value, you need to de-serialize it first. JsonConvert uses Newtonsoft.Json.

string dataObj = "{\"Items\": [{\"id\":1},{\"id\":2}]}";
dynamic data = JsonConvert.DeserializeObject(dataObj );

Then, you can use the code from inside the webapi functions below to create your xml. Below is the code if you are passing this data to webapi in C#.

You can simply use a stringbuilder.

[Route("api/common/JsonToXml")]
        [AcceptVerbs("POST")]
        public HttpResponseMessage JsonToXml(dynamic data)
        {
            StringBuilder str = new StringBuilder();

            str.Append("<Items>");
            for (var ic = 0; ic < data.Items.Count; ic++)
            {
                str.Append("<element><id>");
                str.Append(Convert.ToInt32(data.Items[ic].id));
                str.Append("</id></element>");
            }

            str.Append("</Items>");

            return Request.CreateResponse(HttpStatusCode.OK, Convert.ToString(str));
        }

Or you can define your classes as below. Using Newtonsoft.Json, Serialize and Deserialize.

public class Items
{
    public Items() {
        this.element = new List<Element>();
    }

    public List<Element> element;
}

public class Element
{
    public Element(int id) {
        this.Id = id;
    }

    public int Id;
}

[Route("api/common/JsonToXml")]
        [AcceptVerbs("POST")]
        public HttpResponseMessage JsonToXml(dynamic data)
        {
            Items list = new Items();
            list.element = new List<Element>();

            for (var ic = 0; ic < data.Items.Count; ic++)
            {
                list.element.Add(new Element(Convert.ToInt32(data.Items[ic].id)));
            }

            XmlDocument xmlData = JsonConvert.DeserializeXmlNode(JsonConvert.SerializeObject(list), "Items");

            return Request.CreateResponse(HttpStatusCode.OK, xmlData.OuterXml);
        }
Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
0

Using JsonCovert class you can convert json to XML

String json = "{\"Items\": [{\"id\":1},{\"id\":2}]}";
XmlDocument doc = 
(XmlDocument)JsonConvert.DeserializeXmlNode(json,"element");
MessageBox.Show(doc.InnerXml.ToString());

In your case try like this

            String json = "{\"Items\": [{\"id\":1},{\"id\":2}]}";
            dynamic parse =  JsonConvert.DeserializeObject(json);
            XmlDocument doc = new XmlDocument();
            XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            XmlElement root = doc.DocumentElement;
            doc.InsertBefore(xmlDeclaration, root);
            XmlElement element1 = doc.CreateElement(string.Empty, "root", string.Empty);
            doc.AppendChild(element1);
            XmlElement element2 = doc.CreateElement(string.Empty, "Items", string.Empty);
            element1.AppendChild(element2);
            foreach (var Items in parse.Items)
            {
                XmlElement element22 = doc.CreateElement(string.Empty, "element", string.Empty);
                element2.AppendChild(element22);
                XmlElement element3 = doc.CreateElement(string.Empty, "id", string.Empty);
                XmlText text1 = doc.CreateTextNode(Items.id.ToString());
                element3.AppendChild(text1);
                element22.AppendChild(element3);
            }

            MessageBox.Show(doc.InnerXml.ToString());
Znaneswar
  • 3,329
  • 2
  • 16
  • 24