DataContractSerializer – how can I output the xml to a string (as opposed to a file)

Something like this – put your output into a MemoryStream and then read that back in: public static string DataContractSerializeObject<T>(T objectToSerialize) { using(MemoryStream memStm = new MemoryStream()) { var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(memStm, objectToSerialize); memStm.Seek(0, SeekOrigin.Begin); using(var streamReader = new StreamReader(memStm)) { string result = streamReader.ReadToEnd(); return result; } } }

XSLT Transform XML with Namespaces

You need to provide a namespace prefix in your xslt for the elements you are transforming. For some reason (at least in a Java JAXP parser) you can’t simply declare a default namespace. This worked for me: <xsl:stylesheet version=”1.0″ xmlns:t=”http://www.test.com/” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xslFormatting=”urn:xslFormatting”> <xsl:output method=”html” indent=”no”/> <xsl:template match=”/t:ArrayOfBrokerage”> <xsl:for-each select=”t:Brokerage”> Test </xsl:for-each> </xsl:template> </xsl:stylesheet> This … Read more

“Type not expected”, using DataContractSerializer – but it’s just a simple class, no funny stuff?

The exception that is being reported is for VDB_Sync.Model.Konstant. This means that somewhere further up the chain, this class is being pulled into another class and that class is the one being serialized. The issue is that depending on how Konstant is embedded in this class (for example, if it is in a collection or … Read more

XML Serialization and namespace prefixes

To control the namespace alias, use XmlSerializerNamespaces. [XmlRoot(“Node”, Namespace=”http://flibble”)] public class MyType { [XmlElement(“childNode”)] public string Value { get; set; } } static class Program { static void Main() { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(“myNamespace”, “http://flibble”); XmlSerializer xser = new XmlSerializer(typeof(MyType)); xser.Serialize(Console.Out, new MyType(), ns); } } If you need to change the namespace … Read more

Using DataContractSerializer to serialize, but can’t deserialize back

Here is how I’ve always done it: public static string Serialize(object obj) { using(MemoryStream memoryStream = new MemoryStream()) using(StreamReader reader = new StreamReader(memoryStream)) { DataContractSerializer serializer = new DataContractSerializer(obj.GetType()); serializer.WriteObject(memoryStream, obj); memoryStream.Position = 0; return reader.ReadToEnd(); } } public static object Deserialize(string xml, Type toType) { using(Stream stream = new MemoryStream()) { byte[] data = … Read more

DataContractSerializer doesn’t call my constructor?

DataContractSerializer (like BinaryFormatter) doesn’t use any constructor. It creates the object as empty memory. For example: Type type = typeof(Customer); object obj = System.Runtime.Serialization. FormatterServices.GetUninitializedObject(type); The assumption is that the deserialization process (or callbacks if necessary) will fully initialize it.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)