Ved Prakash Yadav
Ved Prakash Yadav

Reputation: 3

xml to json string without double qoutes in number and decimal type in c#

I have an XElement I am trying to convert into a JSON string. Below is my XElement:

<myxml>
  <gstin>28GSDUH1331G155</gstin>
  <fp>122016</fp>
  <gt>3782969.01</gt>
  <b2b>
    <ctin>29GSDUH1331G155</ctin>
    <inv p2:Array="true">
      <inum>S008502</inum>
      <idt>15-11-2016</idt>
      <val>10000.00</val>
      <pos>27</pos>
      <rchrg>N</rchrg>
      <prs>Y</prs>
      <od_num>8401</od_num>
      <od_dt>14-11-2016</od_dt>
      <etin>30GSDUH1331G155</etin>
      <itms p2:Array="true">
        <num>1</num>
        <itm_det>
          <ty>G</ty>
          <hsn_sc>G1221</hsn_sc>
          <txval>10000.00</txval>
          <crt>1.00</crt>
          <camt>100.00</camt>
          <srt>1.00</srt>
          <samt>100.00</samt>
          <csrt>100.00</csrt>
          <csamt>100.00</csamt>
        </itm_det>
      </itms>
    </inv>
  </b2b>
</myxml>

I am using this string jsonString = JsonConvert.SerializeXNode(myxml); to convert in json string and i am getting the jsonstring

"{\"myxml\":{\"gstin\":\"28GSDUH1331G155\",\"fp\":\"122016\",\"gt\":\"3782969.01\",\"b2b\":[{\"ctin\":\"29GSDUH1331G155\",\"inv\":[{\"inum\":\"S008502\",\"idt\":\"15-11-2016\",\"val\":\"10000.00\",\"pos\":\"27\",\"rchrg\":\"N\",\"prs\":\"Y\",\"od_num\":\"8401\",\"od_dt\":\"14-11-2016\",\"etin\":\"30GSDUH1331G155\",\"itms\":[{\"num\":\"1\",\"itm_det\":{\"ty\":\"G\",\"hsn_sc\":\"G1221\",\"txval\":\"10000.00\",\"crt\":\"1.00\",\"camt\":\"100.00\",\"srt\":\"1.00\",\"samt\":\"100.00\",\"csrt\":\"100.00\",\"csamt\":\"100.00\"}}]}]}]}}"

The problem that i am running into is that integer and decimal values are being surrounded by backslashes and double quotes(\" \") . I don't want backslash double quotes on numbers and decimals.

This is the JSON string which i want as an output.

"{\"myxml\":{\"gstin\":\"28GSDUH1331G155\",\"fp\":122016,\"gt\":3782969.01,\"b2b\":[{\"ctin\":\"29GSDUH1331G155\",\"inv\":[{\"inum\":\"S008502\",\"idt\":\"15-11-2016\",\"val\":10000.00,\"pos\":27,\"rchrg\":\"N\",\"prs\":\"Y\",\"od_num\":8401,\"od_dt\":\"14-11-2016\",\"etin\":\"30GSDUH1331G155\",\"itms\":[{\"num\":1,\"itm_det\":{\"ty\":\"G\",\"hsn_sc\":\"G1221\",\"txval\":10000.00,\"crt\":1.00,\"camt\":100.00,\"srt\":1.00,\"samt\":100.00,\"csrt\":100.00,\"csamt\":100.00}}]}]}]}}"

Upvotes: 0

Views: 960

Answers (1)

confusedandamused
confusedandamused

Reputation: 756

As the comments have already pointed out - more information would likely help (as your XML is currently invalid).

Also in the future it is always best to show Minimal, Complete, and Verifiable examples to the questions you are asking - not only to show that you have tried, but to give people who are helping more information.

When simply inspecting the element in the debugger as you did it does in-fact show backslashes

"{\"myxml\":{\"gstin\":\"28GSDUH1331G155\",\"fp\":\"122016\",\"gt\":\"3782969.01\",\"b2b\":{\"ctin\":\"29GSDUH1331G155\",\"inv\":{\"@Array\":\"true\",\"inum\":\"S008502\",\"idt\":\"15-11-2016\",\"val\":\"10000.00\",\"pos\":\"27\",\"rchrg\":\"N\",\"prs\":\"Y\",\"od_num\":\"8401\",\"od_dt\":\"14-11-2016\",\"etin\":\"30GSDUH1331G155\",\"itms\":{\"@Array\":\"true\",\"num\":\"1\",\"itm_det\":{\"ty\":\"G\",\"hsn_sc\":\"G1221\",\"txval\":\"10000.00\",\"crt\":\"1.00\",\"camt\":\"100.00\",\"srt\":\"1.00\",\"samt\":\"100.00\",\"csrt\":\"100.00\",\"csamt\":\"100.00\"}}}}}}"

These backslashes are just inserted as escape characters and are NOT actually there.

If you inspect the actual values of jsonString you will find your expected value. When you are viewing the jsonString in the debugger you are looking at a raw JSON string, but in the below screenshot you are looking at a representation of a JSON object.

enter image description here

Upvotes: 1

Related Questions