
November 29th, 2007, 12:44 PM
|
|
|
This is what I'm using:
c# Code:
Original
- c# Code |
|
|
|
public static string XmlSerialize(object objectToSerialize, bool emitUtf8Identifier) { XmlSerializer serializer = new XmlSerializer( objectToSerialize.GetType()); // If emitUtf8Identifier is true, the resulting data // will have the UTF-8 file marker at the beginning of the stream. // This is OK for data to be written to a file, but will cause // XmlDocument::LoadXml(serializedData) to fail. UTF8Encoding encoding = new UTF8Encoding(emitUtf8Identifier); using (MemoryStream mem = new MemoryStream()) { using (StreamWriter writer = new StreamWriter(mem, encoding)) { // Write object to the stream. serializer.Serialize(writer, serializable); // Reset the position for string extraction. writer.BaseStream.Position = 0; } return encoding.GetString(mem.ToArray()); } }
|