C # JSON to XML

Source: Internet
Author: User

Using System.IO;
Using System.Text;
Using System.Xml.Serialization;
Using System.Xml;
Using System.Runtime.Serialization.Json;

Namespace Common.core
{
public class Serializationhelper
{
private static string Xmlserialize (object o)
{
XmlSerializer ser = new XmlSerializer (O.gettype ());
System.IO.MemoryStream mem = new MemoryStream ();
XmlTextWriter writer = new XmlTextWriter (mem, Encoding.UTF8);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();
Ns. Add ("", "");
Ser. Serialize (writer, o, NS);
Writer. Close ();
Return Encoding.UTF8.GetString (Mem. ToArray ());
}

private static T xmldeserialize<t> (string s)
{
XmlDocument Xdoc = new XmlDocument ();
Try
{
Xdoc. LOADXML (s);
XmlNodeReader reader = new XmlNodeReader (Xdoc. DocumentElement);
XmlSerializer ser = new XmlSerializer (typeof (T));
Object obj = ser. Deserialize (reader);
Return (T) obj;
}
Catch
{
return default (T);
}
}

private static string Jsonserialize (object o)
{
using (var ms = new MemoryStream ())
{
New DataContractJsonSerializer (O.gettype ()). WriteObject (MS, O);
Return Encoding.UTF8.GetString (Ms. ToArray ());
}
}

private static T jsondeserialize<t> (string s)
{
using (var ms = new MemoryStream (Encoding.UTF8.GetBytes (s)))
{
Return (t) new DataContractJsonSerializer (typeof (T)). ReadObject (MS);
}
}

//<summary>
//Serializes the object according to format (Xml/json) into a string result
//</summary>
//<param name= "O" > Target object and Lt;/param>
//<param name= "format" > Output format </param>
///<returns></returns>
public static string Serialize (object o, format format)
{
if (format = = Format.xml)
{
return serialization Helper.xmlserialize (o);
}
Else
{
return serializationhelper.jsonserialize (o);
}
}

<summary>
Deserializes a string into an object of the specified type according to format (Xml/json)
</summary>
<typeparam name= "T" > Specify Type </typeparam>
<param name= "S" > Target string </param>
<param name= "format" > Input format </param>
<returns></returns>
public static T deserialize<t> (string s, format format)
{
if (format = = Format.xml)
{
return serializationhelper.xmldeserialize<t> (s);
}
Else
{
return serializationhelper.jsondeserialize<t> (s);
}
}
}
}

C # JSON to XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.