{"id":50,"date":"2011-08-01T13:18:00","date_gmt":"2011-08-01T13:18:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2011\/08\/01\/convert-xelements-to-json\/"},"modified":"2011-08-01T13:18:00","modified_gmt":"2011-08-01T13:18:00","slug":"convert-xelements-to-json","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2011\/08\/01\/convert-xelements-to-json\/","title":{"rendered":"Convert XElements to Json"},"content":{"rendered":"<p>You need to capture the data type in a class that is serializable.<br \/>In this example I use Employee to do so. The first action will be to construct an IEnumerable of Employee:<\/p>\n<pre> XElement empXml = GetEmployees();<br \/> var empJson = from emp in empXml.Descendants(\"Employee\")<br \/>               select new Employee<br \/>               {<br \/>                      ID = emp.Element(\"ID\").Value,<br \/>                      FirstName = emp.Element(\"FirstName\").Value,<br \/>                      Department = emp.Element(\"Department\").Value,<br \/>                      City = emp.Element(\"City\").Value<br \/>               };<br \/><\/pre>\n<p>The second step is to serialize the enumeration into the memory stream.<\/p>\n<pre>  var ser = new  DataContractJsonSerializer(typeof(IEnumerable<employee>));<br \/>  var ms = new MemoryStream();<br \/>  ser.WriteObject(ms, empJson);<br \/><\/pre>\n<p>And at last, we can take the generated stream as a string just before closing the stream.<\/p>\n<pre>string json = Encoding.Default.GetString(ms.ToArray());<br \/>ms.Close();<br \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You need to capture the data type in a class that is serializable.In this example I use Employee to do so. The first action will be to construct an IEnumerable of Employee: XElement empXml = GetEmployees(); var empJson = from emp in empXml.Descendants(&#8220;Employee&#8221;) select new Employee { ID = emp.Element(&#8220;ID&#8221;).Value, FirstName = emp.Element(&#8220;FirstName&#8221;).Value, Department = &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2011\/08\/01\/convert-xelements-to-json\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Convert XElements to Json&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[41],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"You need to capture the data type in a class that is serializable.In this example I use Employee to do so. The first action will be to construct an IEnumerable of Employee: XElement empXml = GetEmployees(); var empJson = from emp in empXml.Descendants(\"Employee\") select new Employee { ID = emp.Element(\"ID\").Value, FirstName = emp.Element(\"FirstName\").Value, Department =&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/50"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}