本文整理汇总了C#中System.Runtime.Serialization.NetDataContractSerializer.WriteObject方法的典型用法代码示例。如果您正苦于以下问题:C# NetDataContractSerializer.WriteObject方法的具体用法?C# NetDataContractSerializer.WriteObject怎么用?C# NetDataContractSerializer.WriteObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Runtime.Serialization.NetDataContractSerializer
的用法示例。
在下文中一共展示了NetDataContractSerializer.WriteObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToElement
public XElement ToElement(object val, Func<Type, XNamespace> getNamespace)
{
var serializer = new NetDataContractSerializer();
using (var ms = new MemoryStream())
{
serializer.WriteObject(ms, val);
ms.Seek(0, SeekOrigin.Begin);
return XElement.Load(new XmlTextReader(ms));
}
}
示例2: Serialize
public static byte[] Serialize(object graph)
{
using (MemoryStream memStream = new MemoryStream())
using (GZipStream zipStream = new GZipStream(memStream, CompressionMode.Compress))
{
using (XmlDictionaryWriter xmlDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(zipStream))
{
NetDataContractSerializer serializer = new NetDataContractSerializer();
serializer.WriteObject(xmlDictionaryWriter, graph);
xmlDictionaryWriter.Close();
}
zipStream.Close();
return memStream.ToArray();
}
}
示例3: Serialize
private string Serialize(object value, Type type)
{
using (var m = new MemoryStream())
{
var w = XmlDictionaryWriter.CreateTextWriter(m, Encoding.Unicode);
//var s = new DataContractSerializer(type, null, Int32.MaxValue, false, false, null, new PluginDataContractResolver());
var s = new NetDataContractSerializer();
s.WriteObject(w, value);
w.Flush();
w.Close();
return System.Text.Encoding.Unicode.GetString(m.ToArray());
}
}
示例4: GetSerializedVersion
private static Response GetSerializedVersion(object response)
{
try
{
using (var stream = new MemoryStream())
{
var serializer = new NetDataContractSerializer();
serializer.WriteObject(stream, response);
stream.Position = 0;
return (Response)serializer.ReadObject(stream);
}
}
catch (Exception ex)
{
throw new ColomboSerializationException(string.Format("{0} could not be serialized.", response), ex);
}
}
示例5: WriteCompilerOutput
private static void WriteCompilerOutput(string path, WorkflowCompilerResults results)
{
using (Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
{
XmlWriterSettings settings = new XmlWriterSettings {
Indent = true
};
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
NetDataContractSerializer serializer = new NetDataContractSerializer();
SurrogateSelector selector = new SurrogateSelector();
selector.AddSurrogate(typeof(MemberAttributes), serializer.Context, new CompilerResultsSurrogate());
serializer.SurrogateSelector = selector;
serializer.WriteObject(writer, results);
}
}
}
示例6: Net_data_contract_serialization_will_not_change_the_type_of_a_collection
public void Net_data_contract_serialization_will_not_change_the_type_of_a_collection()
{
using (var session = DataAccess.OpenSession())
{
var forum = session.Get<ForumModel>(1);
//company.EmployeesList made public for the purpose of
//this demo
Assert.AreEqual(typeof(NHibernate.Collection.Generic.PersistentGenericBag<TopicModel>), forum.Topics.GetType());
var serializer = new NetDataContractSerializer();
//serialize company to a memory stream
Stream stream = new MemoryStream();
serializer.WriteObject(stream, forum);
Console.WriteLine();
//deserialize the memory stream back to a company
stream.Position = 0;
forum = (ForumModel)serializer.ReadObject(stream);
Assert.AreEqual(typeof(NHibernate.Collection.Generic.PersistentGenericBag<TopicModel>), forum.Topics.GetType());
}
}
示例7: SerializeObject2
internal ArraySegment<byte> SerializeObject2(object source)
{
ArraySegment<byte> nums = new ArraySegment<byte>(PSWorkflowFileInstanceStore.NullArray);
try
{
XmlObjectSerializer netDataContractSerializer = new NetDataContractSerializer();
using (MemoryStream memoryStream = new MemoryStream())
{
netDataContractSerializer.WriteObject(memoryStream, source);
byte[] numArray = new byte[(IntPtr)(memoryStream.Length + (long)1)];
numArray[0] = 70;
Buffer.BlockCopy(memoryStream.GetBuffer(), 0, numArray, 1, (int)memoryStream.Length);
nums = new ArraySegment<byte>(numArray, 0, (int)numArray.Length);
memoryStream.Close();
}
}
catch (SerializationException serializationException1)
{
SerializationException serializationException = serializationException1;
this.ThrowErrorOrWriteWarning(serializationException);
}
catch (InvalidDataContractException invalidDataContractException1)
{
InvalidDataContractException invalidDataContractException = invalidDataContractException1;
this.ThrowErrorOrWriteWarning(invalidDataContractException);
}
catch (Exception exception1)
{
Exception exception = exception1;
this.Tracer.TraceException(exception);
throw;
}
return nums;
}
示例8: TryNetDataContractSerializer
private bool TryNetDataContractSerializer(Type type)
{
using (var m = new MemoryStream())
{
var w = XmlTextWriter.Create(
m,
new XmlWriterSettings()
{
Indent = true,
Encoding = Encoding.Unicode,
NamespaceHandling = NamespaceHandling.OmitDuplicates,
});
var s = new NetDataContractSerializer(
new StreamingContext(),
int.MaxValue,
false,
System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple,
null);
var o = Activator.CreateInstance(type, true);
s.WriteObject(w, o);
w.Flush();
w.Close();
// Don't forget to skip byte order mark
var buffer = m.ToArray();
var prelen = Encoding.Unicode.GetPreamble().Length;
var xml = System.Text.Encoding.Unicode.GetString(buffer, prelen, buffer.Length - prelen);
}
return true;
}
示例9: SaveResultsToFile
private static void SaveResultsToFile(ScheduledJob job, FileStream fs)
{
XmlObjectSerializer netDataContractSerializer = new NetDataContractSerializer();
netDataContractSerializer.WriteObject(fs, job);
fs.Flush();
}
示例10: SaveSchemas
public static void SaveSchemas(string filename)
{
using ( FileStream fs = new FileStream(filename, FileMode.Create) )
{
XmlWriter xw = XmlWriter.Create(fs);
xw.WriteStartDocument();
xw.WriteStartElement("SchemaDefinitions");
foreach ( var schema in schemas )
{
NetDataContractSerializer sr = new NetDataContractSerializer();
sr.WriteObject(xw, schema.Value);
}
xw.WriteEndElement(); // SchemaDefinitions
xw.WriteEndDocument();
xw.Flush();
}
}
示例11: GetBinarySerializedForm
byte[] GetBinarySerializedForm(object instance)
{
NetDataContractSerializer serializer = new NetDataContractSerializer();
MemoryStream memStr = new MemoryStream();
XmlDictionaryWriter dictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(memStr);
serializer.WriteObject(dictionaryWriter, instance);
dictionaryWriter.Flush();
byte[] bytes = memStr.ToArray();
dictionaryWriter.Close();
memStr.Close();
return bytes;
}
示例12: UpdateJobStore
private void UpdateJobStore()
{
FileStream fileForJobDefinition = null;
try
{
fileForJobDefinition = ScheduledJobStore.GetFileForJobDefinition(this.Name, FileMode.Create, FileAccess.Write, FileShare.None);
XmlObjectSerializer netDataContractSerializer = new NetDataContractSerializer();
netDataContractSerializer.WriteObject(fileForJobDefinition, this);
fileForJobDefinition.Flush();
}
finally
{
if (fileForJobDefinition != null)
{
fileForJobDefinition.Close();
}
}
if (this.Credential != null)
{
this.UpdateFilePermissions(this.Credential.UserName);
}
}
示例13: AddToJobStore
/// <summary>
/// Adds this scheduled job definition to the job definition store.
/// </summary>
private void AddToJobStore()
{
FileStream fs = null;
try
{
fs = ScheduledJobStore.CreateFileForJobDefinition(Name);
_definitionFilePath = ScheduledJobStore.GetJobDefinitionLocation();
_definitionOutputPath = ScheduledJobStore.GetJobRunOutputDirectory(Name);
XmlObjectSerializer serializer = new System.Runtime.Serialization.NetDataContractSerializer();
serializer.WriteObject(fs, this);
fs.Flush();
}
finally
{
if (fs != null)
{
fs.Close();
}
}
// If credentials are provided then update permissions.
if (Credential != null)
{
UpdateFilePermissions(Credential.UserName);
}
}
示例14: SaveResultsToFile
/// <summary>
/// Writes the job (which implements ISerializable) to the provided
/// file stream.
/// </summary>
/// <param name="job">ScheduledJob job to save</param>
/// <param name="fs">FileStream</param>
private static void SaveResultsToFile(ScheduledJob job, FileStream fs)
{
XmlObjectSerializer serializer = new System.Runtime.Serialization.NetDataContractSerializer();
serializer.WriteObject(fs, job);
fs.Flush();
}
示例15: Save
/// <summary>
/// Saves the machine to an output stream.
/// </summary>
/// <param name="output"></param>
public void Save(Stream output)
{
NetDataContractSerializer serializer = new NetDataContractSerializer();
serializer.WriteObject(output, this);
}