Exemplo n.º 1
0
 public static string Serialize <TType>(TType source, params NamespaceMapping[] namespaceMappings)
 {
     using (MemoryStream ms = new MemoryStream()) {
         using (StreamWriter writer = new StreamWriter(ms, Encoding.UTF8)) {
             XmlFragmentWriter tw = new XmlFragmentWriter(writer)
             {
                 Formatting = Formatting.Indented, IndentChar = '\t', Indentation = 1
             };
             System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(typeof(TType));
             if (namespaceMappings != null)
             {
                 XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                 foreach (NamespaceMapping nsm in namespaceMappings)
                 {
                     ns.Add(nsm.Prefix, nsm.Namespace);
                 }
                 s.Serialize(tw, source, ns);
             }
             else
             {
                 s.Serialize(tw, source);
             }
             ms.Position = 0;
             return(new StreamReader(ms).ReadToEnd());
         }
     }
 }
Exemplo n.º 2
0
 public static void Serialize(object source, Stream stream, params NamespaceMapping[] namespaceMappings)
 {
     using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) {
         XmlFragmentWriter tw = new XmlFragmentWriter(writer)
         {
             Formatting = Formatting.Indented, IndentChar = '\t', Indentation = 1
         };
         System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(source.GetType());
         if (namespaceMappings != null)
         {
             XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
             foreach (NamespaceMapping nsm in namespaceMappings)
             {
                 ns.Add(nsm.Prefix, nsm.Namespace);
             }
             s.Serialize(tw, source, ns);
         }
         else
         {
             s.Serialize(tw, source);
         }
     }
 }