简体   繁体   中英

Serialize and Deserialize XML using dot.net c#

I have spent the last few hours putting togeather the following code after reading much that seems out of date or that does'nt quite seem to work.

If its any help to anybody here is the final working code. Free free to comment if it can be improved :-)

public class SerializationHelper<T> {

#region static string SerializeObject( T obj, Encoding encoding )

/// <summary>
///   Serialize an [object] to an Xml String.
/// </summary>
/// <typeparam name="T">Object Type to Serialize</typeparam>
/// <param name="obj">Object Type to Serialize</param>
/// <param name="encoding">System.Text.Encoding Type</param>
/// <returns>Empty.String if Exception, XML string if successful</returns>
/// <example>
///   // UTF-16 Serialize
///   string xml = SerializationHelper<ObjectType>SerializeObject( [object], new UnicodeEncoding( false, false ) );
/// </example>
/// <example>
///   // UTF-8 Serialize
///   string xml = SerializationHelper<ObjectType>SerializeObject( [object], Encoding.UTF8 );
/// </example> 
public static string SerializeObject( T obj, Encoding encoding ) {

  if ( obj == null ) { return string.Empty; }

  try {

    XmlSerializer xmlSerializer = new XmlSerializer( typeof( T ) );

    using ( MemoryStream memoryStream = new MemoryStream() ) {

      XmlWriterSettings xmlWriterSettings = new XmlWriterSettings() { Encoding = encoding };

      using ( XmlWriter writer = XmlWriter.Create( memoryStream, xmlWriterSettings ) ) {

        xmlSerializer.Serialize( writer, obj );

      }

      return encoding.GetString( memoryStream.ToArray() );

    }

  }
  catch {

    return string.Empty;

  }

}

#endregion   

#region static T DeserializeObject( string xml, Encoding encoding )

/// <summary>
///   Deserialize an Xml String to an [object]
/// </summary>
/// <typeparam name="T">Object Type to Deserialize</typeparam>
/// <param name="xml">Xml String to Deserialize</param>
/// <param name="encoding">System.Text.Encoding Type</param>
/// <returns>Default if Exception, Deserialize object if successful</returns>
/// <example>
///   // UTF-16 Deserialize
///   [object] = SerializationHelper<ObjectType>DeserializeObject( xml, Encoding.Unicode )
/// </example>
/// <example>
///   // UTF-8 Deserialize
///   [object] = SerializationHelper<ObjectType>DeserializeObject( xml, Encoding.UTF8 )
/// </example> 
public static T DeserializeObject( string xml, Encoding encoding ) {

  if ( string.IsNullOrEmpty( xml ) ) { return default( T ); }

  try {

    XmlSerializer xmlSerializer = new XmlSerializer( typeof( T ) );

    using ( MemoryStream memoryStream = new MemoryStream( encoding.GetBytes( xml ) ) ) {

      // No settings need modifying here
      XmlReaderSettings  xmlReaderSettings  = new XmlReaderSettings();

      using ( XmlReader xmlReader = XmlReader.Create( memoryStream, xmlReaderSettings ) ) {

        return (T)xmlSerializer.Deserialize( xmlReader );

      }

    }

  }
  catch {

    return default( T );

  }

}

#endregion     

}

I suggest moving the type parameter T to the enclosing class and making the XmlSerializer instance static . A static field in a generic class is per closed type, so SerializationHelper<Apple> and SerializationHelper<Orange> will each have separate instances of the field.

Also, I'm not sure that catch { return String.Empty; } catch { return String.Empty; } is the best idea either -- masking problems to avoid crashing makes me nervous.

I think there is no need for the whole Encoding part. You simply serialise using one encoding, then convert to bytes, and then convert back to Unicode. Why is that? But I might be missing something here.

Another thing that hits me is .ToArray() usage. If you have big hiearchy and serialising lot of objects, this can be pretty performance heavy. Try using StreamReader to read the memory stream without need to copy it into to Array. But this requires some performance testing to back up my reasoning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM