Using the XmlConvert Class


Some characters aren't valid in XML documents. XML documents use XSD types, which are different from 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 a DataTable. The EncodeName method is the reverse of DecodeName: It converts an ADO.NET object to a 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-9 shows the conversion from a Boolean and DateTime object to XML values.

Listing 6-9: Using the XmlConvert Class

start example
 Dim writer As XmlTextWriter = _     New XmlTextWriter("c:\\test.xml", Nothing)     writer.WriteStartElement("MyTestElements")     Dim bl As Boolean = True     writer.WriteElementString("TestBoolean", _     XmlConvert.ToString(bl))     Dim dt As DateTime = New DateTime(2000, 1, 1)     writer.WriteElementString("TestDate", _     XmlConvert.ToString(dt))     writer.WriteEndElement()     writer.Flush()     writer.Close() 
end example




Applied ADO. NET(c) Building Data-Driven Solutions
Applied ADO.NET: Building Data-Driven Solutions
ISBN: 1590590732
EAN: 2147483647
Year: 2006
Pages: 214

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net