Xml serialization – Hide null values

You can create a function with the pattern ShouldSerialize{PropertyName} which tells the XmlSerializer if it should serialize the member or not. For example, if your class property is called MyNullableInt you could have public bool ShouldSerializeMyNullableInt() { return MyNullableInt.HasValue; } Here is a full sample public class Person { public string Name {get;set;} public int? … Read more

Proper way to implement IXmlSerializable?

Yes, GetSchema() should return null. IXmlSerializable.GetSchema Method This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return a null reference (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the XmlSchemaProviderAttribute to the class. For both read and write, the … Read more

Is it possible to deserialize XML into List?

You can encapsulate the list trivially: using System; using System.Collections.Generic; using System.Xml.Serialization; [XmlRoot(“user_list”)] public class UserList { public UserList() {Items = new List<User>();} [XmlElement(“user”)] public List<User> Items {get;set;} } public class User { [XmlElement(“id”)] public Int32 Id { get; set; } [XmlElement(“name”)] public String Name { get; set; } } static class Program { static … Read more

Convert XML String to Object

You need to use the xsd.exe tool which gets installed with the Windows SDK into a directory something similar to: C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin And on 64-bit computers: C:\Program Files (x86)\Microsoft SDKs\Windows\v6.0A\bin And on Windows 10 computers: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin On the first run, you use xsd.exe and you convert your sample XML into a … Read more

{” was not expected.} Deserializing Twitter XML

Either decorate your root entity with the XmlRoot attribute which will be used at compile time. [XmlRoot(Namespace = “www.contoso.com”, ElementName = “MyGroupName”, DataType = “string”, IsNullable=true)] Or specify the root attribute when de serializing at runtime. XmlRootAttribute xRoot = new XmlRootAttribute(); xRoot.ElementName = “user”; // xRoot.Namespace = “http://www.cpandl.com”; xRoot.IsNullable = true; XmlSerializer xs = new … Read more

Serialize an object to XML

You have to use XmlSerializer for XML serialization. Below is a sample snippet. XmlSerializer xsSubmit = new XmlSerializer(typeof(MyObject)); var subReq = new MyObject(); var xml = “”; using(var sww = new StringWriter()) { using(XmlWriter writer = XmlWriter.Create(sww)) { xsSubmit.Serialize(writer, subReq); xml = sww.ToString(); // Your XML } } As per @kiquenet request for generic class: … Read more

Serialize an object to string

Use a StringWriter instead of a StreamWriter: public static string SerializeObject<T>(this T toSerialize) { XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); using(StringWriter textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, toSerialize); return textWriter.ToString(); } } Note, it is important to use toSerialize.GetType() instead of typeof(T) in XmlSerializer constructor: if you use the first one the code covers all possible … Read more