๐Ÿ˜œ

์ญˆ๋‚˜์•„๋น  ๋ธ”๋กœ๊ทธ

JUNA
STUDIO

[C#] How to Serialize XML to JSON Using Json.NET

๋ฐœํ–‰์ผ: Jan, 2025
์กฐํšŒ์ˆ˜: 3
๋‹จ์–ด์ˆ˜: 76

Table of Contents

Introduction

 These days, when exchanging messages between different apps or services, complex structured data is mostly converted to JSON. Of course, there are still many places that use XML. Therefore, there are cases where the data received in XML needs to be serialized to JSON and stored or sent to another node.

 In such cases, you can easily serialize it with just a method call using Json.NET.


Serialization

string xml = @"<?xml version='1.0' standalone='no'?>
<root>
    <person id='1'>
    <name>Alan</name>
    <url>http://www.google.com</url>
    </person>
    <person id='2'>
    <name>Louis</name>
    <url>http://www.yahoo.com</url>
    </person>
</root>";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

string json = JsonConvert.SerializeXmlNode(doc);

Console.WriteLine(json);
// {
//   "?xml": {
//     "@version": "1.0",
//     "@standalone": "no"
//   },
//   "root": {
//     "person": [
//       {
//         "@id": "1",
//         "name": "Alan",
//         "url": "http://www.google.com"
//       },
//       {
//         "@id": "2",
//         "name": "Louis",
//         "url": "http://www.yahoo.com"
//       }
//     ]
//   }
// }

Original text: https://www.newtonsoft.com/json/help/html/ConvertXmlToJson.htm


Tags: #JSON#XML#serialization#Json.NET#data exchange#structured data
JUNA BLOG VISITORS
Today
7
 (
updown
-7
)
Total
657
 (
updown
+7
)

ยฉ 2025 juniyunapapa@gmail.com.