private void SerializeToStream <T>(T data, Func <XmlWriterSettings, XmlWriter> createXmlWriter) { IXmlNamespaceProvider namespaceProvider = data as IXmlNamespaceProvider; IEnumerable <string> namespaces = namespaceProvider != null?namespaceProvider.GetNamespaces() : Enumerable.Empty <string>(); XmlSerializerNamespaces xmlSerializerNamespaces = xmlNamespaceBuilder.Create(namespaces); var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); XmlWriterSettings xmlWriterSettings = new XmlWriterSettings { Encoding = Encoding.UTF8, NamespaceHandling = NamespaceHandling.OmitDuplicates }; using (XmlWriter writer = createXmlWriter(xmlWriterSettings)) { if (data is IHasStyleSheets) { xmlProcessingInstructionHandler.AddStyleSheets(writer, data as IHasStyleSheets); } xmlSerializer.Serialize(writer, data, xmlSerializerNamespaces); } }
public string Serialize <T>(T data) { IXmlNamespaceProvider namespaceProvider = data as IXmlNamespaceProvider; IEnumerable <string> namespaces = namespaceProvider != null?namespaceProvider.GetNamespaces() : Enumerable.Empty <string>(); var xmlSerializerNamespaces = _xmlNamespaceBuilder.Create(namespaces); var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); using (MemoryStream memoryStream = new MemoryStream()) { XmlWriterSettings xmlWriterSettings = new XmlWriterSettings { Encoding = Encoding.UTF8, NamespaceHandling = NamespaceHandling.OmitDuplicates }; using (XmlWriter writer = XmlWriter.Create(memoryStream, xmlWriterSettings)) { xmlSerializer.Serialize(writer, data, xmlSerializerNamespaces); writer.Flush(); memoryStream.Seek(0, SeekOrigin.Begin); return(new StreamReader(memoryStream).ReadToEnd()); } } }
public void SerializeToStream <T>(T data, Stream stream) { IXmlNamespaceProvider namespaceProvider = data as IXmlNamespaceProvider; IEnumerable <string> namespaces = namespaceProvider != null?namespaceProvider.GetNamespaces() : Enumerable.Empty <string>(); XmlSerializerNamespaces xmlSerializerNamespaces = _xmlNamespaceBuilder.Create(namespaces); var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); XmlWriterSettings xmlWriterSettings = new XmlWriterSettings { Encoding = Encoding.UTF8, NamespaceHandling = NamespaceHandling.OmitDuplicates }; using (XmlWriter writer = XmlWriter.Create(stream, xmlWriterSettings)) { xmlSerializer.Serialize(writer, data, xmlSerializerNamespaces); } }
private void SerializeToStream <T>(T data, Func <XmlWriterSettings, XmlWriter> createXmlWriter) { IEnumerable <string> strs; IXmlNamespaceProvider xmlNamespaceProvider = (object)data as IXmlNamespaceProvider; strs = (xmlNamespaceProvider != null ? xmlNamespaceProvider.GetNamespaces() : Enumerable.Empty <string>()); XmlSerializerNamespaces xmlSerializerNamespace = this._xmlNamespaceBuilder.Create(strs); System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); using (XmlWriter xmlWriter = createXmlWriter(new XmlWriterSettings() { Encoding = Encoding.UTF8, NamespaceHandling = NamespaceHandling.OmitDuplicates })) { if ((object)data is IHasStyleSheets) { this._xmlProcessingInstructionHandler.AddStyleSheets(xmlWriter, (object)data as IHasStyleSheets); } xmlSerializer.Serialize(xmlWriter, data, xmlSerializerNamespace); } }
public SiteMapService(IActionResultFactory actionResultFactory, IBaseUrlProvider baseUrlProvider, IXmlNamespaceProvider xmlNamespaceProvider) { this.actionResultFactory = actionResultFactory; this.baseUrlProvider = baseUrlProvider; this.xmlNamespaceProvider = xmlNamespaceProvider; }