Skip to content

Instantly share code, notes, and snippets.

@gfody
Last active July 19, 2019 17:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfody/d7d450dc54a15e586d5b6ab9794073a4 to your computer and use it in GitHub Desktop.
Save gfody/d7d450dc54a15e586d5b6ab9794073a4 to your computer and use it in GitHub Desktop.
converts json string to XmlElement, no System.Runtime.Serialization dependency
XmlElement JsonToXml(string json, string root = "root", string unnamed_array_item = "item")
{
var r = new Regex(@"\s*([\[\]{}]|""([^""]*)""\s*:|(true|false|null|-?\d+(?:\.\d+)?|""[^""]*"")\s*),?", RegexOptions.Multiline | RegexOptions.Compiled);
var doc = new XmlDocument(); var n = doc.CreateElement(root); var s = new Stack<XmlElement>(); string name = null, value = "";
var m = r.Match(json);
while (m.Success)
{
if (m.Groups[1].Value == "]") name = null;
else if (m.Groups[1].Value == "[") name = name ?? unnamed_array_item;
else if (m.Groups[1].Value.EndsWith(":")) name = m.Groups[2].Value;
else if (m.Groups[1].Value == "{" && name != null) { s.Push(n); n = doc.CreateElement(name); }
else if (m.Groups[1].Value == "}" && s.Count > 0) { var p = s.Pop(); p.AppendChild(n); name = n.Name; n = p; }
else if ((value = m.Groups[3].Value) != "")
{
var x = doc.CreateElement(name ?? unnamed_array_item);
if (value.ToLower() != "null") x.AppendChild(doc.CreateTextNode(value.Trim(new char[] { '"' })));
n.AppendChild(x);
}
m = m.NextMatch();
}
return n.FirstChild != null && n.FirstChild.NextSibling == null ? (XmlElement)n.FirstChild : n;
}
@gfody
Copy link
Author

gfody commented May 25, 2016

RFC 7159 samples 1 & 2:

JSON:                                                         | XML:
--------------------------------------------------------------+-------------------------------------------------------
{                                                             | <Image>
  "Image": {                                                  |    <Width>800</Width>
      "Width":  800,                                          |    <Height>600</Height>
      "Height": 600,                                          |    <Title>View from 15th Floor</Title>
      "Title":  "View from 15th Floor",                       |    <Thumbnail>
      "Thumbnail": {                                          |       <Url>http://www.example.com/image/481989943</Url>
          "Url":    "http://www.example.com/image/481989943", |       <Height>125</Height>
          "Height": 125,                                      |       <Width>100</Width>
          "Width":  100                                       |    </Thumbnail>
      },                                                      |    <Animated>false</Animated>
      "Animated" : false,                                     |    <IDs>116</IDs>
      "IDs": [116, 943, 234, 38793]                           |    <IDs>943</IDs>
    }                                                         |    <IDs>234</IDs>
}                                                             |    <IDs>38793</IDs>
                                                              | </Image>
JSON:                               | XML:
------------------------------------+------------------------------------------
[                                   | <root>
   {                                |    <item>
      "precision": "zip",           |       <precision>zip</precision>
      "Latitude":  37.7668,         |       <Latitude>37.7668</Latitude>
      "Longitude": -122.3959,       |       <Longitude>-122.3959</Longitude>
      "Address":   "",              |       <Address></Address>
      "City":      "SAN FRANCISCO", |       <City>SAN FRANCISCO</City>
      "State":     "CA",            |       <State>CA</State>
      "Zip":       "94107",         |       <Zip>94107</Zip>
      "Country":   "US"             |       <Country>US</Country>
   },                               |    </item>
   {                                |    <item>
      "precision": "zip",           |       <precision>zip</precision>
      "Latitude":  37.371991,       |       <Latitude>37.371991</Latitude>
      "Longitude": -122.026020,     |       <Longitude>-122.026020</Longitude>
      "Address":   "",              |       <Address></Address>
      "City":      "SUNNYVALE",     |       <City>SUNNYVALE</City>
      "State":     "CA",            |       <State>CA</State>
      "Zip":       "94085",         |       <Zip>94085</Zip>
      "Country":   "US"             |       <Country>US</Country>
   }                                |    </item>
]                                   | </root>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment