Subversion Repository Public Repository

ChrisCompleteCodeTrunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?xml version="1.0" encoding="utf-8"?>
<topic id="ConvertingJSONandXML" revisionNumber="1">
  <developerConceptualDocument xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:xlink="http://www.w3.org/1999/xlink">
    <introduction>
      <para>Json.NET supports converting JSON to XML and vice versa using the
      <codeEntityReference>T:Newtonsoft.Json.Converters.XmlNodeConverter</codeEntityReference>.</para>
      <para>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.</para>
    </introduction>
    <section>
      <title>Conversion Rules</title>
      <content>
   
<list class="bullet">
  <listItem><para>Elements remain unchanged.</para></listItem>
  <listItem><para>Attributes are prefixed with an @ and should be at the start of the object.</para></listItem>
  <listItem><para>Single child text nodes are a value directly against an element, otherwise they are accessed via #text.</para></listItem>
  <listItem><para>The XML declaration and processing instructions are prefixed with ?.</para></listItem>
  <listItem><para>Character data, comments, whitespace and significant whitespace nodes are accessed via
  #cdata-section, #comment, #whitespace and #significant-whitespace respectively.</para></listItem>
  <listItem><para>Multiple nodes with the same name at the same level are grouped together into an array.</para></listItem>
  <listItem><para>Empty elements are null.</para></listItem>
</list>

<para>If the XML created from JSON doesn't match what you want, then you will need to convert it manually.
The best approach to do this is to load your JSON into a LINQ to JSON object like JObject or JArray and then use LINQ to create
an XDocument. The opposite process, using LINQ with an XDocument to create a JObject or JArray, also works.
You can find out more about using LINQ to JSON with LINQ <link xlink:href="QueryingLINQtoJSON">here</link>.</para>

<alert class="note">
  <para>The version of Json.NET being used in your application will change what XML conversion methods are available.
  SerializeXmlNode/DeserializeXmlNode are available when the framework supports XmlDocument;
  SerializeXNode/DeserializeXNode are available when the framework supports XDocument.</para>
</alert>

      </content>
    </section>
    <section>
      <title>SerializeXmlNode</title>
      <content>
   
       <para>The JsonConvert has two helper methods for converting between JSON and XML. The first is
       <codeEntityReference>Overload:Newtonsoft.Json.JsonConvert.SerializeXmlNode</codeEntityReference>.
       This method takes an XmlNode and serializes it to JSON text.</para>

<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\ConvertingJsonAndXmlTests.cs" region="SerializeXmlNode" title="Converting XML to JSON with SerializeXmlNode" />

       <para>Because multiple nodes with the same name at the same level are grouped together into an array,
       the conversion process can produce different JSON depending on the number of nodes. For example, if some
       XML for a user has a single <codeInline>&lt;Role&gt;</codeInline> node, then that role will be text against
       a JSON <codeInline>"Role"</codeInline> property, but if the user has multiple <codeInline>&lt;Role&gt;</codeInline>
       nodes, then the role values will be placed in a JSON array.</para>
       
       <para>To fix this situation a custom XML attribute can be added to force a JSON array to be created.</para>

<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\ConvertingJsonAndXmlTests.cs" region="ForceJsonArray" title="Attribute to Force a JSON Array" />
    </content>
    </section>
    <section>
      <title>DeserializeXmlNode</title>
      <content>
   
       <para>The second helper method on JsonConvert is
       <codeEntityReference>Overload:Newtonsoft.Json.JsonConvert.DeserializeXmlNode</codeEntityReference>.
       This method takes JSON text and deserializes it into an XmlNode.</para>
   
       <para>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.</para>

<code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\ConvertingJsonAndXmlTests.cs" region="DeserializeXmlNode" title="Converting JSON to XML with DeserializeXmlNode" />

      </content>
    </section>
    <relatedTopics>
      <codeEntityReference>T:Newtonsoft.Json.Converters.XmlNodeConverter</codeEntityReference>
      <codeEntityReference>T:Newtonsoft.Json.JsonConvert</codeEntityReference>
    </relatedTopics>
  </developerConceptualDocument>
</topic>

Commits for ChrisCompleteCodeTrunk/M3Workflow/Libraries/Json90r1/Source/Doc/ConvertingJSONandXML.aml

Diff revisions: vs.
Revision Author Commited Message
1 BBDSCHRIS picture BBDSCHRIS Wed 22 Aug, 2018 20:08:03 +0000