The XmlConvert class

This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".

There are some characters that are not valid in XML documents. XML documents use XSD types, which are different than CLR (.NET) data types. The XmlConvert class contains methods to convert from CLR types to XSD types and vice versa. The DecodeName method transfers an XML name into an ADO.NET object such as DataTable. The EncodeName Method is the reverse of DecodeName: it converts an ADO.NET object to valid XSD name. It takes any invalid character and replaces it with an escape string. Another method, EncodeLocalNAme, converts unpermitted names to valid names.

Besides these three methods, the XmlConvert class has many methods to convert from a string object to Boolean. Byte, integer, and so on. Listing 6-15 shows the conversion from Boolean and Date Time object to XML values.

Listing 6-15 xml convert example


using
System;
using
System.Xml;

class
XmlReaderSamp
{
    static void Main(string[] args)
    {
        XmlTextWriter writer = new XmlTextWriter(
@"C:\Documents and Settings\PuranMAC\My Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\XMLFile1.xml", null);
        writer.WriteStartElement("MyTestElements");
        bool b1 = true;
        writer.WriteElementString("TestBoolean", XmlConvert.ToString(b1));
        DateTime dt = new DateTime(2000, 01, 01);
        writer.WriteElementString("test date", XmlConvert.ToString(dt));
        writer.WriteEndElement();
        writer.Flush();
        writer.Close();
    }
}


Conclusion

Hope this article would have helped you in understanding XmlConvert Class in ADO.NET. See other articles on the website also for further reference.

adobook.jpg This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.

Up Next
    Ebook Download
    View all
    Learn
    View all