Converting CLR types to XML Schema data types can be a painful process if you are not careful. After all, XML data types defined in the XML Schema specification (found here) do not always match up perfectly with their corresponding types in the CLR. However, by using the XmlConvert class in .NET this conversion process is greatly simplified. XmlConvert ensures that CLR types are properly converted to their corresponding XML schema types and vice versa.
This sample application demonstrates how to use the XmlConvert class to convert from XML Schema data types to corresponding CLR types (and vice versa), and shows how to use the EncodeName() method to ensure that dynamically created XML element and attribute names do not contain prohibited characters (as defined in the XML 1.0 specification).