public SurrogateForIXmlSerializable(IXmlSerializable serializable) { assemblyQualifiedName = serializable.GetType().AssemblyQualifiedName; using(var stream = new MemoryStream()) { var xmlSerializer = new XmlSerializer(serializable.GetType()); xmlSerializer.Serialize(stream, serializable); bytes = stream.ToArray(); } }
public static void Serialize(string filePath, IXmlSerializable obj) { XmlSerializer serializer = new XmlSerializer(obj.GetType()); using (StreamWriter writer = new StreamWriter(filePath)) serializer.Serialize(writer, obj); }
private string ToSerializable(IXmlSerializable serializable) { if (serializable == null) { return(null); } try { using (var memory = new MemoryStream()) using (var writer = XmlWriter.Create(memory)) { writer.WriteStartDocument(); serializable.WriteXml(writer); writer.WriteEndDocument(); writer.Flush(); memory.Seek(0L, SeekOrigin.Begin); return(Encoding.UTF8.GetString(memory.GetBuffer(), 0, (int)memory.Length)); } } catch (Exception e) { Log.ErrorException(e, "Error serializing object of type {0}", serializable.GetType().FullName); return(null); } }
public static void Deserialize(IXmlSerializable source, XmlReader reader) { const string method = "Deserialize"; if (reader == null) { throw new Exceptions.NullParameterException(typeof(XmlSerializer), method, "reader"); } // IXmlSerializable.ReadOuterXml() should read the entire element on which the reader is position, // including the EndElement node, which some implementations don't do. Check for this common problem. if (source != null) { #if DEBUG Debug.Assert(reader.NodeType == XmlNodeType.None || reader.NodeType == XmlNodeType.Element, string.Format("The reader node is {0} '{1}' before calling ReadOuterXml() on '{2}'.", reader.NodeType, reader.LocalName, source.GetType().FullName), "Before ReadOuterXml() is called the reader should be positioned at the start of the element" + " to be read by that method."); int depth = reader.Depth; #endif source.ReadOuterXml(reader); #if DEBUG Debug.Assert(depth == 0 || reader.Depth == depth - 1, string.Format("The reader node is {0} '{1}'" + " after calling ReadOuterXml() on '{2}'.", reader.NodeType, reader.LocalName, source.GetType().FullName), "After ReadOuterXml() is called the reader should be positioned immediately AFTER the element" + " read by that method (NOT on the EndElement node of the element that was read)."); #endif } }
public void Serialize(IXmlSerializable item, XmlWriter output) { if (item == null) { throw new ArgumentNullException("value"); } if (output == null) { throw new ArgumentNullException("output"); } XmlRootAttribute xmlRootAttr = GetXmlRootAttribute(item.GetType()); XmlRootPrefixedAttribute xmlRootPrefixAttr = xmlRootAttr as XmlRootPrefixedAttribute; if (xmlRootPrefixAttr != null && xmlRootPrefixAttr.Prefix.HasValue()) { output.WriteStartElement(xmlRootPrefixAttr.Prefix, xmlRootAttr.ElementName, xmlRootAttr.Namespace); } else { output.WriteStartElement(xmlRootAttr.ElementName, xmlRootAttr.Namespace); } item.WriteXml(output); output.WriteEndElement(); }
public static string WriteXml(IXmlSerializable target) { StringBuilder sb = new StringBuilder(); XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.ConformanceLevel = ConformanceLevel.Fragment; using (XmlWriter writer = XmlTextWriter.Create(sb, xmlWriterSettings)) { XmlRootAttribute[] xmlRootAttribute = target.GetType().GetCustomAttributes(typeof(XmlRootAttribute), false) as XmlRootAttribute[]; writer.WriteStartElement(xmlRootAttribute[0].ElementName); target.WriteXml(writer); writer.WriteEndElement(); } return sb.ToString(); }
public ContestResults(string contestGuid, string techniqueName, string techniqueDescription, List<DatasetResultsDTO> results, double score, IXmlSerializable baseData) { ContestGUID = contestGuid; TechniqueName = techniqueName; TechniqueDescription = techniqueDescription; Results = results; Score = score; // var serializer = TraceLab.Core.Serialization.XmlSerializerFactory.GetSerializer(baseData.GetType(), null); using (MemoryStream memorystream = new MemoryStream()) { serializer.Serialize(memorystream, baseData); string xmlObject = Encoding.Default.GetString(memorystream.ToArray()); BaseData = xmlObject; } }
private static void SerializeXmlSerializable(IXmlSerializable obj, XContainer parent) { StringBuilder sb = new StringBuilder(); using (XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings { ConformanceLevel = ConformanceLevel.Fragment })) { obj.WriteXml(xw); xw.Flush(); } Type objType = obj.GetType(); string contentName = null; object[] attrs = objType.GetCustomAttributes(typeof(XmlRootAttribute), true); if (attrs.Length > 0) { contentName = ((XmlRootAttribute)attrs[0]).ElementName; } if (String.IsNullOrEmpty(contentName)) { contentName = objType.Name; } using (XmlReader xr = XmlReader.Create(new StringReader(sb.ToString()), new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment, CloseInput = true })) { if (!xr.Read()) { return; } XElement content = new XElement(contentName); while (!xr.EOF) { content.Add(XNode.ReadFrom(xr)); } parent.Add(content); } parent.Add(new XAttribute(XmlSerializer.AttributeFormat, XmlSerializer.AttributeValueCustom)); }
public ContestResults(string contestGuid, string techniqueName, string techniqueDescription, List <DatasetResultsDTO> results, double score, IXmlSerializable baseData) { ContestGUID = contestGuid; TechniqueName = techniqueName; TechniqueDescription = techniqueDescription; Results = results; Score = score; // var serializer = TraceLab.Core.Serialization.XmlSerializerFactory.GetSerializer(baseData.GetType(), null); using (MemoryStream memorystream = new MemoryStream()) { serializer.Serialize(memorystream, baseData); string xmlObject = Encoding.Default.GetString(memorystream.ToArray()); BaseData = xmlObject; } }
public void Serialize(IXmlSerializable item, XmlWriter output) { if (item == null) throw new ArgumentNullException("value"); if (output == null) throw new ArgumentNullException("output"); XmlRootAttribute xmlRootAttr = GetXmlRootAttribute(item.GetType()); XmlRootPrefixedAttribute xmlRootPrefixAttr = xmlRootAttr as XmlRootPrefixedAttribute; if (xmlRootPrefixAttr != null && xmlRootPrefixAttr.Prefix.HasValue()) { output.WriteStartElement(xmlRootPrefixAttr.Prefix, xmlRootAttr.ElementName, xmlRootAttr.Namespace); } else { output.WriteStartElement(xmlRootAttr.ElementName, xmlRootAttr.Namespace); } item.WriteXml(output); output.WriteEndElement(); }