vatsal
vatsal

Reputation: 262

Issue while convert JSON to XML with force Array enabled

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>

Upvotes: 3

Views: 362

Answers (1)

Prany
Prany

Reputation: 2143

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" } ]}]}]}}

Upvotes: 2

Related Questions