Serialize and Deserialize XML using dot.net c#

≡放荡痞女 提交于 2019-12-19 11:47:26

问题


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     

}

回答1:


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; } is the best idea either -- masking problems to avoid crashing makes me nervous.




回答2:


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.



来源:https://stackoverflow.com/questions/5025365/serialize-and-deserialize-xml-using-dot-net-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!