简体   繁体   中英

Convert Json format to xml format

I have the query to select data:

Public Function GetStaffList(StaffCode As String) As IEnumerable
   Dim query = (From c In Staff Where c.StaffID= StaffCode Select c)
   Return query
End Function

I found this to work:

    string xml = "";
    string json = @"{
                    '?xml': {
                      '@version': '1.0',
                      '@standalone': 'no'
                    },
                    'root': {
                    'object': " + JsonConvert.SerializeObject(object, Formatting.None)
                    + "}}";
    var xd = JsonConvert.DeserializeXmlNode(json);
    using (var sw = new StringWriter()) {
      using (var xw = System.Xml.XmlWriter.Create(sw)) {
        xd.WriteTo(xw);
        xw.Flush();
        xml = sw.GetStringBuilder().ToString();
      }
    }

The problem you may run into in using DeserializeXmlNode is that it will convert long decimals to scientific notation

{
  "imp_currency_exchange": [{
      "base_currency_code": "USD",
      "exchange_currency_code": "IDR",
      "exchange_date": "2022-02-01T16:06:50Z",
      "exchange_rate": 14205,
      "batch_id": "FXR-20220201_3",
      "reverse_exchange_rate": 0.0000703977
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM