Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- using System.Xml.Serialization;
- namespace ConsoleApplication3
- {
- public static class XmlExtension
- {
- public static T DeSerialize<T>(this T value, Byte[] bytes)
- {
- if (value == null)
- return default(T);
- var xmlserializer = new XmlSerializer(typeof(T));
- var xmlSerializerNamespaces = new XmlSerializerNamespaces();
- xmlSerializerNamespaces.Add(string.Empty, string.Empty);
- using (var reader = new MemoryStream(bytes))
- {
- value = (T)xmlserializer.Deserialize(reader);
- }
- return value;
- }
- public static string Serialize<T>(this T value)
- {
- if (value == null)
- return string.Empty;
- var xmlserializer = new XmlSerializer(typeof(T));
- var xmlSerializerNamespaces = new XmlSerializerNamespaces();
- xmlSerializerNamespaces.Add(string.Empty, string.Empty);
- using (var stringWriter = new StringWriter())
- {
- using (var writer = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true, Encoding = Encoding.UTF8 }))
- {
- xmlserializer.Serialize(writer, value, xmlSerializerNamespaces);
- return stringWriter.ToString();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement