a575963da9
Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
146 lines
11 KiB
HTML
146 lines
11 KiB
HTML
<html>
|
|
|
|
<head>
|
|
<title>Converting between JSON and XML</title>
|
|
<link href="styles.css" rel="stylesheet" type="text/css" />
|
|
<link href="custom.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="control">
|
|
<span class="productTitle">Json.NET - Quick Starts & API Documentation</span><br />
|
|
<span class="topicTitle">Converting between JSON and XML</span></div>
|
|
|
|
<div id="content">
|
|
<span style="color: DarkGray"> </span>
|
|
<p>Json.NET supports converting JSON to XML and vice versa using the
|
|
<a href="./html/T_Newtonsoft_Json_Converters_XmlNodeConverter.htm">XmlNodeConverter</a>.</p>
|
|
|
|
<p>
|
|
Elements, attributes, text, comments, character data, processing instructions,
|
|
namespaces and the XML declaration are all preserved when converting between the
|
|
two. The only caveat is that it is possible to lose the order of differently
|
|
named nodes at the same level when they are grouped together into an array.</p>
|
|
<h3>Conversion Rules</h3>
|
|
<ul>
|
|
<li>Elements remain unchanged. </li>
|
|
<li>Attributes are prefixed with an @ and should be at the start of the object. </li>
|
|
<li>Single child text nodes are a value directly against an element, otherwise they
|
|
are accessed via #text. </li>
|
|
<li>The XML declaration and processing instructions are prefixed with ?. </li>
|
|
<li>Charater data, comments, whitespace and significate whitespace nodes are
|
|
accessed via #cdata-section, #comment, #whitespace and #significate-whitespace
|
|
respectively. </li>
|
|
<li>Multiple nodes with the same name at the same level are grouped together into an
|
|
array. </li>
|
|
<li>Empty elements are null.</li>
|
|
</ul>
|
|
|
|
<h3>SerializeXmlNode</h3>
|
|
<p>The JsonConvert has two helper methods for converting between JSON and XML. The first is <a href="./html/M_Newtonsoft_Json_JsonConvert_SerializeXmlNode.htm">SerializeXmlNode</a>. This method takes an
|
|
XmlNode and serializes it to JSON text.</p>
|
|
<div class="overflowpanel">
|
|
<div class="code">
|
|
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
|
<pre style="margin: 0px;"><span style="color: blue;">string</span> xml = <span style="color: #a31515;">@"<?xml version=""1.0"" standalone=""no""?></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"><root></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> <person id=""1""></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> <name>Alan</name></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> <url>http://www.google.com</url></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> </person></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> <person id=""2""></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> <name>Louis</name></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> <url>http://www.yahoo.com</url></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> </person></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"></root>"</span>;</pre>
|
|
<pre style="margin: 0px;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: #2b91af;">XmlDocument</span> doc = <span style="color: blue;">new</span> <span style="color: #2b91af;">XmlDocument</span>();</pre>
|
|
<pre style="margin: 0px;">doc.LoadXml(xml);</pre>
|
|
<pre style="margin: 0px;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: blue;">string</span> jsonText = <span style="color: #2b91af;">JsonConvert</span>.SerializeXmlNode(doc);</pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">//{</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// "?xml": {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// "@version": "1.0",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// "@standalone": "no"</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// },</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// "root": {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// "person": [</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// "@id": "1",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// "name": "Alan",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// "url": "http://www.google.com"</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// },</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// "@id": "2",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// "name": "Louis",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// "url": "http://www.yahoo.com"</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// ]</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// }</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">//}</span></pre>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<h3>DeserializeXmlNode</h3>
|
|
|
|
<p>The second helper method on JsonConvert is <a href="./html/M_Newtonsoft_Json_JsonConvert_DeserializeXmlNode.htm">DeserializeXmlNode</a>. This method takes
|
|
JSON text and deserializes it into a XmlNode.</p>
|
|
<p>Because valid XML must have one root element the JSON passed to
|
|
DeserializeXmlNode should have one property in the root JSON object. If the root
|
|
JSON object has multiple properties then the overload that also takes an element
|
|
name should be used. A root element with that name will be inserted into the
|
|
deserialized XmlNode.</p>
|
|
|
|
<div class="overflowpanel">
|
|
|
|
<div class="code">
|
|
|
|
<div style="font-family: Courier New; font-size: 10pt; color: black;">
|
|
<pre style="margin: 0px;"><span style="color: blue;">string</span> json = <span style="color: #a31515;">@"{</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""?xml"": {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@version"": ""1.0"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@standalone"": ""no""</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""root"": {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""person"": [</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@id"": ""1"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""name"": ""Alan"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://www.google.com""</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> },</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> {</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""@id"": ""2"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""name"": ""Louis"",</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ""url"": ""http://www.yahoo.com""</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> ]</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;"> }</span></pre>
|
|
<pre style="margin: 0px;"><span style="color: #a31515;">}"</span>;</pre>
|
|
<pre style="margin: 0px;"> </pre>
|
|
<pre style="margin: 0px;"><span style="color: #2b91af;">XmlDocument</span> doc = (<span style="color: #2b91af;">XmlDocument</span>)<span style="color: #2b91af;">JsonConvert</span>.DeserializeXmlNode(json);</pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// <?xml version="1.0" standalone="no"?></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// <root></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// <person id="1"></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// <name>Alan</name></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// <url>http://www.google.com</url></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// </person></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// <person id="2"></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// <name>Louis</name></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// <url>http://www.yahoo.com</url></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// </person></span></pre>
|
|
<pre style="margin: 0px;"><span style="color: green;">// </root></span></pre>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div id="footer"></div>
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html> |