<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-166843388529951045</id><updated>2024-03-14T11:57:05.674+05:30</updated><category term="Generic functions"/><category term="Generic function for json conversion.JSON"/><category term="JSON Conversion"/><category term="Json to object conversion in c#"/><category term="Object to Xml conversion"/><category term="XML"/><category term="XML converter"/><category term="XML converter in c#"/><category term="XML in c#"/><title type='text'>IShareIdea</title><subtitle type='html'>Some use full tips for programming</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ishareidea.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/166843388529951045/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://ishareidea.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Syam S</name><uri>http://www.blogger.com/profile/16611639718639360771</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-166843388529951045.post-8913793709674427048</id><published>2012-05-22T13:48:00.002+05:30</published><updated>2012-05-22T19:03:05.618+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Generic functions"/><category scheme="http://www.blogger.com/atom/ns#" term="Object to Xml conversion"/><category scheme="http://www.blogger.com/atom/ns#" term="XML"/><category scheme="http://www.blogger.com/atom/ns#" term="XML converter"/><category scheme="http://www.blogger.com/atom/ns#" term="XML converter in c#"/><category scheme="http://www.blogger.com/atom/ns#" term="XML in c#"/><title type='text'>Object to XML Conversion in C#</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Sample XML is given below&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot;?&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;lt;note&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;lt;to&amp;gt;Sumesh&amp;lt;/to&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;lt;from&amp;gt;Ramesh&amp;lt;/from&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;lt;heading&amp;gt;greetings&amp;lt;/heading&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;lt;body&amp;gt;How are you doing&amp;lt;/body&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;lt;/note&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
XML is widely using in data exchange , here is code for generic function for converting object to xml&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp;public static string ToXML(this object o, Type t)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; string XmlizedString = null;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; MemoryStream memoryStream = new MemoryStream();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; XmlSerializer xs = new XmlSerializer(t);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; xs.Serialize(xmlTextWriter, o);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; memoryStream = (MemoryStream)xmlTextWriter.BaseStream;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return XmlizedString;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp;private static String UTF8ByteArrayToString(Byte[] characters)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; UTF8Encoding encoding = new UTF8Encoding();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; String constructedString = encoding.GetString(characters);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return (constructedString);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;Feeds&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ishareidea.blogspot.com/feeds/8913793709674427048/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ishareidea.blogspot.com/2012/05/object-to-xml.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/166843388529951045/posts/default/8913793709674427048'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/166843388529951045/posts/default/8913793709674427048'/><link rel='alternate' type='text/html' href='http://ishareidea.blogspot.com/2012/05/object-to-xml.html' title='Object to XML Conversion in C#'/><author><name>Syam S</name><uri>http://www.blogger.com/profile/16611639718639360771</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-166843388529951045.post-2027044501265915318</id><published>2012-05-22T12:58:00.000+05:30</published><updated>2012-05-22T13:01:43.773+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Generic function for json conversion.JSON"/><category scheme="http://www.blogger.com/atom/ns#" term="Generic functions"/><category scheme="http://www.blogger.com/atom/ns#" term="JSON Conversion"/><category scheme="http://www.blogger.com/atom/ns#" term="Json to object conversion in c#"/><title type='text'>JSON Conversion</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;






&lt;span style=&quot;color: blue; font-size: large;&quot;&gt;&lt;u&gt;Conversion of JSON string to object using generic function in&amp;nbsp;&lt;/u&gt;&lt;/span&gt;&lt;u style=&quot;color: blue; font-size: x-large;&quot;&gt;&amp;nbsp;c#&amp;nbsp;&lt;/u&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is using widely in data exchange&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;






Generic functions&amp;nbsp;&lt;/h3&gt;
&lt;div&gt;
Generic methods have type parameters. They provide a way to parameterize the types used in a method. This means you can provide only one implementation and call it with different types. Generic methods require an unusual syntax form for more&amp;nbsp;information&amp;nbsp;on generic methods&amp;nbsp;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/twcad0zb(v=vs.80).aspx&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Click here&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;using System.Runtime.Serialization.Json;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;public static &amp;nbsp;class Utilities&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #0b5394;&quot;&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&amp;nbsp;&lt;span style=&quot;color: #3d85c6;&quot;&gt; public static T Deserialize&amp;lt;T&amp;gt;(string jsonString)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (T)serializer.ReadObject(ms);&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;color: #6fa8dc;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
Above&amp;nbsp;function&amp;nbsp;accept string and return type and convert it to object as specified in return type.&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
Calling generic &amp;nbsp;function&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;List&amp;lt;.RequestsType&amp;gt;objLstResponse=new&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;List&amp;lt;.RequestsType&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;objLstResponse=&lt;/span&gt;&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;span style=&quot;text-align: left;&quot;&gt;Utilities&lt;/span&gt;.Deserialise&amp;lt;List&amp;lt;.RequestsType&amp;gt;&amp;gt;(response);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;span style=&quot;color: #3d85c6;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;Feeds&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ishareidea.blogspot.com/feeds/2027044501265915318/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ishareidea.blogspot.com/2012/05/json-conversion.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/166843388529951045/posts/default/2027044501265915318'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/166843388529951045/posts/default/2027044501265915318'/><link rel='alternate' type='text/html' href='http://ishareidea.blogspot.com/2012/05/json-conversion.html' title='JSON Conversion'/><author><name>Syam S</name><uri>http://www.blogger.com/profile/16611639718639360771</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry></feed>