Working with the XmlConvert Class

Encoding Names:

The output shown below is read from an ArrayList. Any "incorrect" XML names (according to the definitions supplied in the XML specification) are converted to a proper XML format by the XmlConvert class.

Non-Encoded Name: XML Encoded Name:
Postal CodePostal_x0020_Code
Customer NameCustomer_x0020_Name
Customer AddressCustomer_x0020_Address
1Email_x0031_Email
Address*Address_x002A_
Product&NumberProduct_x0026_Number

Converting from XML to CLR Types:

XmlConvert allows XML values to be converted directly to CLR types. This example shows the conversion of the dateTime type defined in the XML Schema specification to the DateTime CLR type. Upon conversion, the different DateTime types are compared to see who is the youngest and oldest.

Customers:
John Doe  (DOB:1/5/1940)
Jane Doe  (DOB:4/15/1945)
Jimmy Doe  (DOB:7/15/1970)

Oldest customer's birthdate: 1/5/1940
Youngest customer's birthdate: 7/15/1970

Converting CLR Types to XML Types:

XmlConvert allows CLR types to be converted directly to XML types (as defined in the XML Schema specification). This example using the XmlTextWriter and XmlConvert classes for the conversion of a DateTime CLR type to a dateTime XML type. The XmlConvert class's ToString() method is called which automatically handles converting the CLR DateTime type to the correct XML dateTime type (year-month-day).

XmlTextWriter Output: