NHibernate: How do I XmlSerialize an ISet?

NHibernate serialization has been treated a lot on stackoverflow. See: C# Castle ActiveRecord: How to elegantly (XML) serialize ActiveRecord objects? How do I serialize all properties of an NHibernate-mapped object? NHibernate and WCF Serialization(Unidirectional) JSON.NET and nHibernate Lazy Loading of Collections Which .NET JSON serializers can deal with NHibernate proxy objects? DTOs vs Serializing Persisted … Read more

Serializing interfaces

No. You can’t serialize an interface. Ever. It just told you that. An interface is nothing more than a description of a set of behaviors. It says nothing about the contents of an instance. In particular, although an instance of a class implementing an interface must implement all of its members, it will certainly have … Read more

How can I transform XML into a List or String[]?

It sounds like you’re more after just parsing rather than full XML serialization/deserialization. If you can use LINQ to XML, this is pretty easy: using System; using System.Linq; using System.Xml.Linq; public class Test { static void Main() { string xml = “<Ids><id>1</id><id>2</id></Ids>”; XDocument doc = XDocument.Parse(xml); var list = doc.Root.Elements(“id”) .Select(element => element.Value) .ToList(); foreach … Read more

XmlSerializer Performance Issue when Specifying XmlRootAttribute

Just for anyone else who runs into this problem; armed with the answer above and the example from MSDN I managed to resolve this issue using the following class: public static class XmlSerializerCache { private static readonly Dictionary<string, XmlSerializer> cache = new Dictionary<string, XmlSerializer>(); public static XmlSerializer Create(Type type, XmlRootAttribute root) { var key = … Read more

How do I specify XML serialization attributes to support namespace prefixes during deserialization in .NET?

You’ll need to indicate which namespaces each field requires by using Namespace of XmlElement attribute. This will allow you to associate a field with a particular namespace, but you’ll also need to expose a property on your class that returns type XmlNamespaceDeclarations in order to get the prefix association. See documentation and sample below: [XmlRoot(ElementName … Read more

How to rename XML attribute that generated after serializing List of objects

The most reliable way is to declare an outermost DTO class: [XmlRoot(“myOuterElement”)] public class MyOuterMessage { [XmlElement(“item”)] public List<TestObject> Items {get;set;} } and serialize that (i.e. put your list into another object). You can avoid a wrapper class, but I wouldn’t: class Program { static void Main() { XmlSerializer ser = new XmlSerializer(typeof(List<Foo>), new XmlRootAttribute(“Flibble”)); … Read more

XML Serialization question – How to Serialize Element, Attribute and Text from One Object

[XmlText], like so: using System; using System.Xml.Serialization; [Serializable, XmlRoot(“myElement”)] public class MyType { [XmlAttribute(“name”)] public string Name {get;set;} [XmlText] public string Text {get;set;} } static class Program { static void Main() { new XmlSerializer(typeof(MyType)).Serialize(Console.Out, new MyType { Name = “foo”, Text = “bar” }); } }