Null coalescing operator (??) example in C# and using it with LINQ & XML

The ?? operator can be very useful in these scenarios. Continue reading


XmlRoot, XmlIgnore and XmlAttribute for XML serialization


XmlIgnore: If you specify XmlIgnore on a field, that field will be ignored when serialized.
XmlAttribute:This Specifies that the XmlSerializer must serialize the class member as an XML attribute.
XmlRoot:Controls XML serialization of the attribute target as an XML root element. You can use the word 
XmlRoot in your code instead of the longer XmlRootAttribute.

XmlElementattribute:  Indicates that a public field or property represents an XML element when the XmlSerializer serializes or deserializes the object that contains it. Set the element name and namespace of the XML element.  By applying an XmlElementAttribute to an array, you instruct   the XmlSerializer to serialize the array as a series of XML  elements, instead of a nested set of elements. Continue reading

C# – Convert XML String to Object

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

On the first run, you use xsd.exe and you convert your sample XML into a XSD file (XML schema file):

xsd yourfile.xml

This gives you yourfile.xsd, which in a second step, you can convert again using xsd.exe into a C# class:

xsd yourfile.xsd /c

This should give you a file yourfile.cs which will contain a C# class that you can use to deserialize the XML file you’re getting – something like:

XmlSerializer serializer = new XmlSerializer(typeof(msg));
msg resultingMessage = (msg)serializer.Deserialize('yourfile.xml');