13.4 Using XmlConvert to Convert XML Data Types

 <  Day Day Up  >  

13.4 Using XmlConvert to Convert XML Data Types

You want to ensure that XML elements and attributes do not contain invalid characters . You additionally want to convert XML strings into different Common Language Runtime ( CLR ) data types.


Technique

You use the XmlConvert class to manipulate and translate the strings contained within elements and attributes of an XML file. To ensure a string does not contain invalid characters, pass the original string into the static method EncodeName defined in the XmlConvert class. It returns a valid XML-equivalent string. To convert the string back to its original form, use the DecodeName method in the same manner:

 
 private void tvXML_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) {     string newName = XmlConvert.EncodeName( e.Label );     string originalName = XmlConvert.DecodeName( newName ); } 

You can also use the XmlConvert class to convert XML strings into .NET data types and vice versa. The conversion methods are modeled closely after the methods defined in the Convert class. For instance, to convert a string extracted from an XML document into a DateTime equivalent, call the static method ToDateTime defined in the XmlConvert class, passing in the string to convert. If the conversion cannot occur due to formatting issues, you have to catch the FormatException exception:

 
 public void ConvertXMLString( string xmlString ) {     DateTime dt;     try     {         dt = XmlConvert.ToDateTime( xmlString );         Console.WriteLine( "Converted string = {0}", dt.ToString() );     }     catch( FormatException e )     {         Console.WriteLine( "Cannot convert \"{0}\" to DateTime", xmlString );     } } 

Comments

XML has a certain range of valid characters that it can use, as defined in the XML 1.0 (Second Edition) recommendation. Some of the invalid characters, for instance, are spaces, the ampersand (&), and the asterisk (*). Because of this limitation, the XmlConvert class was created to allow you to convert arbitrary strings into well- formed and valid XML string equivalents. In most cases, the hexadecimal value of each invalid character is inserted into the string. For instance, if you call the EncodeName method using the string "An Element" , the resultant string is "An_x0020_Element" . As you can see, the space character was replaced by its Unicode-based hexadecimal value, surrounded by two underscore characters.

In the application that was created for this chapter, a simple XML editor, XML elements are placed within an editable TreeView control. The user is able to click on a tree node's label and change its value, which internally changes the element value within the XML document. However, without any sort of checking or conversion, you have the potential for the user to input an invalid string. Therefore, the XmlConvert class is used whenever an AfterLabelEdit event is thrown. Within the event handler, the new text is converted using EncodeName to ensure the XML document remains valid.

The other function provided by the XmlConvert class, as mentioned earlier, is to convert XML strings to .NET data types or the other way around. If you recall from earlier chapters, you use the Convert class to convert data types. So you might be wondering why you even have to bother with the XmlConvert class because the Convert class already provides this functionality. In most cases, you are probably safe using the Convert methods. However, the XmlConvert class was designed to work with data types specified in XML schemas. In other words, the date data type in the XML schema might not necessarily be the same as the DateTime data type in the .NET framework. Therefore, if you pass an XML schema “based string representing a date time value into the Convert method ToDateTime , you might receive a FormatException . Again, for simple data types like integers, you might be safe. However, when working with XML string conversions, you might as well use the XmlConvert as an extra layer of protection.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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