简体   繁体   English

使用dot.net C#序列化和反序列化XML

[英]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 . 我建议将类型参数T移到封闭类中,并使XmlSerializer实例为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. 泛型类中的静态字段是每个封闭类型的,因此SerializationHelper<Apple>SerializationHelper<Orange>将分别具有该字段的单独实例。

Also, I'm not sure that catch { return String.Empty; } 另外,我不确定是否catch { return String.Empty; } catch { return String.Empty; } is the best idea either -- masking problems to avoid crashing makes me nervous. catch { return String.Empty; }还是最好的主意-掩盖问题以避免崩溃使我感到紧张。

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. 您只需使用一种编码进行序列化,然后转换为字节,然后再转换回Unicode。 Why is that? 这是为什么? But I might be missing something here. 但是我可能在这里错过了一些东西。

Another thing that hits me is .ToArray() usage. 令我震惊的另一件事是.ToArray()用法。 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. 尝试使用StreamReader读取内存流,而无需将其复制到Array中。 But this requires some performance testing to back up my reasoning. 但这需要进行一些性能测试以支持我的推理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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