3

In my work, I need to convert XML to JSON and again same JSON converts to XML so for that, I am using NewtonSoft.Json (Version=6.0.0.0) in my C# Code.

I need to force the single node to be an array for that I am using same XML structure as you can see in newtonsoft JSON's site.

I am using following code to convert JSON to XML.

 XmlDocument xmlDoc = JsonConvert.DeserializeXmlNode(JSON, "", true);

While i am converting following JSON into XML it will Add an attribute like xmlns:json='http://james.newtonking.com/projects/json' & json:Array='true' in XML.Resulting XML is also added.

{"Test_Service" : {"fname":"mark","lname":"joye","CarCompany":"saab","CarNumber":"9741","IsInsured":"true","safty":["ABS","AirBags","childdoorlock"],"CarDescription":"test Car","collections":[{"XYZ":"1","PQR":"11","contactdetails":[{"contname":"DOM","contnumber":"8787"},{"contname":"COM","contnumber":"4564","addtionaldetails":[{"description":"54657667"}]},{"contname":"gf","contnumber":"123","addtionaldetails":[{"description":"123"}]}]}]}}

<?xml version="1.0"?>
<Test_Service>
    <fname>mark</fname>
    <lname>joye</lname>
    <CarCompany>saab</CarCompany>
    <CarNumber>9741</CarNumber>
    <IsInsured>true</IsInsured>
    <safty>ABS</safty>
    <safty>AirBags</safty>
    <safty>childdoorlock</safty>
    <CarDescription>test Car</CarDescription>
    <collections
        xmlns:json="http://james.newtonking.com/projects/json" json:Array="true">
        <XYZ>1</XYZ>
        <PQR>11</PQR>
        <contactdetails>
            <contname>DOM</contname>
            <contnumber>8787</contnumber>
        </contactdetails>
        <contactdetails>
            <contname>COM</contname>
            <contnumber>4564</contnumber>
            <addtionaldetails json:Array="true">
                <description>54657667</description>
            </addtionaldetails>
        </contactdetails>
        <contactdetails>
            <contname>gf</contname>
            <contnumber>123</contnumber>
            <addtionaldetails json:Array="true">
                <description>123</description>
            </addtionaldetails>
        </contactdetails>
    </collections>
</Test_Service>

But if I am using following JSON with ns3 tag (mention below) and try to convert JSON to XML after converting it will not adding an attribute like xmlns:json='http://james.newtonking.com/projects/json' & json:Array='true' in converted XML. Converted XML is added below.

{"ns3:Test_Service" : {"@xmlns:ns3":"http://www.CCKS.org/XRT/Form","ns3:fname":"mark","ns3:lname":"joye","ns3:CarCompany":"saab","ns3:CarNumber":"9741","ns3:IsInsured":"true","ns3:safty":["ABS","AirBags","childdoorlock"],"ns3:CarDescription":"test Car","ns3:collections":[{"ns3:XYZ":"1","ns3:PQR":"11","ns3:contactdetails":[{"ns3:contname":"DOM","ns3:contnumber":"8787"},{"ns3:contname":"COM","ns3:contnumber":"4564","ns3:addtionaldetails":[{"ns3:description":"54657667"}]},{"ns3:contname":"gf","ns3:contnumber":"123","ns3:addtionaldetails":[{"ns3:description":"123"}]}]}]}}

<?xml version="1.0"?>
<ns3:Test_Service
    xmlns:ns3="http://www.CCKS.org/XRT/Form">
    <ns3:fname>mark</ns3:fname>
    <ns3:lname>joye</ns3:lname>
    <ns3:CarCompany>saab</ns3:CarCompany>
    <ns3:CarNumber>9741</ns3:CarNumber>
    <ns3:IsInsured>true</ns3:IsInsured>
    <ns3:safty>ABS</ns3:safty>
    <ns3:safty>AirBags</ns3:safty>
    <ns3:safty>childdoorlock</ns3:safty>
    <ns3:CarDescription>test Car</ns3:CarDescription>
    <ns3:collections>
        <ns3:XYZ>1</ns3:XYZ>
        <ns3:PQR>11</ns3:PQR>
        <ns3:contactdetails>
            <ns3:contname>DOM</ns3:contname>
            <ns3:contnumber>8787</ns3:contnumber>
        </ns3:contactdetails>
        <ns3:contactdetails>
            <ns3:contname>COM</ns3:contname>
            <ns3:contnumber>4564</ns3:contnumber>
            <ns3:addtionaldetails>
                <ns3:description>54657667</ns3:description>
            </ns3:addtionaldetails>
        </ns3:contactdetails>
        <ns3:contactdetails>
            <ns3:contname>gf</ns3:contname>
            <ns3:contnumber>123</ns3:contnumber>
            <ns3:addtionaldetails>
                <ns3:description>123</ns3:description>
            </ns3:addtionaldetails>
        </ns3:contactdetails>
    </ns3:collections>
</ns3:Test_Service>
vatsal
  • 262
  • 2
  • 13
  • I'm finding it difficult to understand exactly what you want vs what you're getting. Please could you provide a [mcve]? – Jon Skeet Jun 07 '18 at 07:24
  • Maybe this helps: https://stackoverflow.com/questions/4056419/json-corresponding-to-an-xml-with-attributes – Rumpelstinsk Jun 07 '18 at 07:25
  • @DaisyShipton Please check my updated question you will get the idea. – vatsal Jun 07 '18 at 09:18
  • @Rumpelstinsk Please check my updated question you will get the idea. – vatsal Jun 07 '18 at 09:19
  • You've now shown a lot of XML (more than is necessary, I strongly suspect) but still only a single line of code. This still isn't a [mcve]. I want to be able to help you, but you're making it harder than it needs to be. – Jon Skeet Jun 07 '18 at 09:21
  • @DaisyShipton yes this is single line code I am using newtonsoft.json which handles everything to convert JSON to XML. – vatsal Jun 07 '18 at 09:27
  • But that's not a single line of code we can run without anything else. When I'm asking for a [mcve], I'm asking for something where I can copy/paste/compile/run. While I *could* spend 5 minutes building that myself, it's more appropriate IMO for the questioner to do so. That way even if 10 people want to answer, only one person has taken the time to make it as easy to reproduce as possible - and that's the person who will benefit most. – Jon Skeet Jun 07 '18 at 09:40
  • (And as well as the code, there's the matter of data. Do we need 30+ lines of XML to see the problem? I suspect not.) – Jon Skeet Jun 07 '18 at 09:43

1 Answers1

1

You are trying to merge two XML namespaces which is wrong, if you remove ns3 from your collections tag and its child tags then you'll see the result. Please see attached screenshot. I've tested this. enter image description here

and below is your corrected JSON

{"ns3:Test_Service": {"@xmlns:ns3": "http://www.CCKS.org/XRT/Form","ns3:fname": "mark","ns3:lname": "joye","ns3:CarCompany": "saab","ns3:CarNumber": "9741","ns3:IsInsured": "true","ns3:safty": [ "ABS", "AirBags", "childdoorlock" ],"ns3:CarDescription": "test Car","collections": [{"XYZ": "1","PQR": "11","contactdetails": [{"contname": "DOM","contnumber": "8787"},{"contname": "COM","contnumber": "4564","addtionaldetails": [ { "description": "54657667" } ]},{"contname": "gf","contnumber": "123","addtionaldetails": [ { "description": "123" } ]}]}]}}
Prany
  • 2,078
  • 2
  • 13
  • 31