System.Xml.XmlConvert Class

Assembly: System.Xml.dll
Namespace: System.Xml
Summary
Encodes and decodes XML names and provides methods for converting between common language runtime types and XML Schema definition language (XSD) types. When converting data types the values returned are locale independent.
C# Syntax:
public class XmlConvert
Remarks
Element and attribute names or ID values are limited to a range of XML characters according to the Extensible Markup Language (XML) 1.0 (Second Edition) recommendation, located at www.w3.org/TR/2000/REC-xml-20001006.html. When names contain invalid characters, the XmlConvert.EncodeName and XmlConvert.DecodeName methods are used to translate them into valid XML names.

Many languages and applications such as Microsoft SQL Server and Microsoft Word, allow Unicode characters in their names, which are not valid in XML names. For example, if 'Order Detail' were a column heading in a database, the database allows the space between the words Order and Detail. However, in XML, the space between Order and Detail is considered an invalid XML character. Thus, the space, the invalid character, needs to be converted into an escaped hexadecimal encoding and can be decoded later.

The EncodeName method can be used with the XmlTextWriter class to ensure the names being written are valid XML names. The following C# code converts the name 'Order Detail' into a valid XML name and writes the element <Order_0x0020_Detail>My order</Order_0x0020_Detail> .

          writer.WriteElementString(XmlConvert.EncodeName("Order Detail"),"My order");
        

XmlConvert also provides methods that enable you to convert from a string to a .NET Framework data type and vice-versa. Locale settings are not taken into account during data conversion. The data types are based on the XML Schema (XSD) data types. The table found at the conceptual topic at MSDN: datatypesupportbetweenxsdtypesnetframeworktypes describes the mapping between XML Schema (XSD) and .NET data types.

In the following example, the XmlTextReader uses one of the XmlConvert.ToDouble method to read in data and convert it from a String to a Double.

          Double price = XmlConvert.ToDouble(reader.Value);
        
See also:
System.Xml Namespace

System.Xml.XmlConvert Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Public Methods
DecodeName Decodes a name. This method does the reverse of the XmlConvert.EncodeName and XmlConvert.EncodeLocalName methods.
EncodeLocalName Converts the name to a valid XML local name.
EncodeName Converts the name to a valid XML name.
EncodeNmToken Verifies the name is valid according to the XML specification.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToBoolean Converts the String to a Boolean equivalent.
ToByte Converts the String to a Byte equivalent.
ToChar Converts the String to a Char equivalent.
ToDateTime Overloaded:
ToDateTime(string s)

Converts the String to a DateTime equivalent.
ToDateTime Overloaded:
ToDateTime(string s, string format)

Converts the String to a DateTime equivalent.
ToDateTime Overloaded:
ToDateTime(string s, string[] formats)

Converts the String to a DateTime equivalent.
ToDecimal Converts the String to a Decimal equivalent.
ToDouble Converts the String to a Double equivalent.
ToGuid Converts the String to a Guid equivalent.
ToInt16 Converts the String to a Int16 equivalent.
ToInt32 Converts the String to a Int32 equivalent.
ToInt64 Converts the String to a Int64 equivalent.
ToSByte Converts the String to a SByte equivalent.
ToSingle Converts the String to a Single equivalent.
ToString
(inherited from System.Object)
Overloaded:
ToString()

See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
ToString Overloaded:
ToString(bool value)

Converts the Boolean to a String.
ToString Overloaded:
ToString(byte value)

Converts the Byte to a String.
ToString Overloaded:
ToString(char value)

Converts the Char to a String.
ToString Overloaded:
ToString(DateTime value)

Converts the DateTime to a String.
ToString Overloaded:
ToString(decimal value)

Converts the Decimal to a String.
ToString Overloaded:
ToString(double value)

Converts the Double to a String.
ToString Overloaded:
ToString(Guid value)

Converts the Guid to a String.
ToString Overloaded:
ToString(short value)

Converts the Int16 to a String.
ToString Overloaded:
ToString(int value)

Converts the Int32 to a String.
ToString Overloaded:
ToString(long value)

Converts the Int64 to a String.
ToString Overloaded:
ToString(sbyte value)

Converts the SByte to a String.
ToString Overloaded:
ToString(float value)

Converts the Single to a String.
ToString Overloaded:
ToString(TimeSpan value)

Converts the TimeSpan to a String.
ToString Overloaded:
ToString(ushort value)

Converts the UInt16 to a String.
ToString Overloaded:
ToString(uint value)

Converts the UInt32 to a String.
ToString Overloaded:
ToString(ulong value)

Converts the UInt64 to a String.
ToString Overloaded:
ToString(DateTime value, string format)

Converts the DateTime to a String.
ToTimeSpan Converts the String to a TimeSpan equivalent.
ToUInt16 Converts the String to a UInt16 equivalent.
ToUInt32 Converts the String to a UInt32 equivalent.
ToUInt64 Converts the String to a UInt64 equivalent.
VerifyName Verifies that the name is a valid name according to the W3C Extended Markup Language recommendation.
VerifyNCName Verifies that the name is a valid NCName according to the W3C Extended Markup Language recommendation.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Xml.XmlConvert Member Details

ctor #1
Summary:
Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public XmlConvert();

Return to top


Method: DecodeName(
   string name
)
Summary
Decodes a name. This method does the reverse of the XmlConvert.EncodeName and XmlConvert.EncodeLocalName methods.
C# Syntax:
public static string DecodeName(
   string name
);
Parameters:

name

The name to be transformed.

Return Value:
The decoded name.
Remarks
The names are decoded using the following rules:

Note The actual encoding of the character is application-specific. For example, Order_x0020_Details becomes Order Details. Even escaped characters that are invalid in XML names will be recognized and decoded.
Example
The following example encodes and decodes names.
using System;
using System.IO;
using System.Xml;

public class Sample
{

  public static void Main()
  {

     // Encode and decode a name with spaces.
     string name1 = XmlConvert.EncodeName("Order Detail");
     Console.WriteLine("Encoded name: " + name1);
     Console.WriteLine("Decoded name: " + XmlConvert.DecodeName(name1));

     // Encode and decode a local name.
     string name2 = XmlConvert.EncodeLocalName("a:book");
     Console.WriteLine("Encoded local name: " + name2);
     Console.WriteLine("Decoded local name: " + XmlConvert.DecodeName(name2));

  }
}

    

Return to top


Method: EncodeLocalName(
   string name
)
Summary
Converts the name to a valid XML local name.
C# Syntax:
public static string EncodeLocalName(
   string name
);
Parameters:

name

The name to be encoded.

Return Value:
The encoded name.
Remarks
This method is similar to the XmlConvert.EncodeName method except that it encodes the colon character, which guarantees that the name can be used as the local name part of a namespace qualified name.

For example, if you passed this method the invalid name a:b, it returns a_x003a_b, which is a valid local name.

If name is null or String.Empty then you get the same value returned.

Example
The following example encodes and decodes names.
using System;
using System.IO;
using System.Xml;

public class Sample
{

  public static void Main()
  {

     // Encode and decode a name with spaces.
     string name1 = XmlConvert.EncodeName("Order Detail");
     Console.WriteLine("Encoded name: " + name1);
     Console.WriteLine("Decoded name: " + XmlConvert.DecodeName(name1));

     // Encode and decode a local name.
     string name2 = XmlConvert.EncodeLocalName("a:book");
     Console.WriteLine("Encoded local name: " + name2);
     Console.WriteLine("Decoded local name: " + XmlConvert.DecodeName(name2));

  }
}

    
See also:
XmlConvert.EncodeName | XmlConvert.DecodeName

Return to top


Method: EncodeName(
   string name
)
Summary
Converts the name to a valid XML name.
C# Syntax:
public static string EncodeName(
   string name
);
Parameters:

name

A name to be translated.

Return Value:
Returns the name with any invalid characters replaced by an escape string.
Remarks
This method translates invalid characters, such as spaces or half-width Katakana, that need to be mapped to XML names without the support or presence of schemas. The invalid characters are translated into escaped numeric entity encodings.

The escape character is "_". Any XML name character that does not conform to the W3C Extensible Markup Language (XML) 1.0 specification is escaped as _xHHHH_. The HHHH string stands for the four-digit hexadecimal UCS-2 code for the character in most significant bit first order. For example, the name Order Details is encoded as Order_x0020_Details.

The underscore character does not need to be escaped unless it is followed by a character sequence that together with the underscore can be misinterpreted as an escape sequence when decoding the name. For example, Order_Details is not encoded, but Order_x0020_ is encoded as Order_x005f_x0020_. No shortforms are allowed. For example, the forms _x20_ and __ are not generated.

This method guarantees the name is valid according to the XML specification. It allows colons in any position, which means the name may still be invalid according to the W3C Namespace Specification (www.w3.org/TR/REC-xml-names). To guarantee it is a valid namespace qualified name use XmlConvert.EncodeLocalName for the prefix and local name parts and join the result with a colon.

Example
The following example encodes and decodes names.
using System;
using System.IO;
using System.Xml;

public class Sample
{

  public static void Main()
  {

     // Encode and decode a name with spaces.
     string name1 = XmlConvert.EncodeName("Order Detail");
     Console.WriteLine("Encoded name: " + name1);
     Console.WriteLine("Decoded name: " + XmlConvert.DecodeName(name1));

     // Encode and decode a local name.
     string name2 = XmlConvert.EncodeLocalName("a:book");
     Console.WriteLine("Encoded local name: " + name2);
     Console.WriteLine("Decoded local name: " + XmlConvert.DecodeName(name2));

  }
}

    
See also:
XmlConvert.EncodeLocalName | XmlConvert.DecodeName

Return to top


Method: EncodeNmToken(
   string name
)
Summary
Verifies the name is valid according to the XML specification.
C# Syntax:
public static string EncodeNmToken(
   string name
);
Parameters:

name

The name to be encoded.

Return Value:
The encoded name.
Remarks
This method guarantees that the name is valid according to the XML specification. For example, if you passed this method the invalid name 70+, it returns 70_x002b_ which is a valid XML name.

If name is null or String.Empty then you get the same value returned.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~XmlConvert();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToBoolean(
   string s
)
Summary
Converts the String to a Boolean equivalent.
C# Syntax:
public static bool ToBoolean(
   string s
);
Parameters:

s

The string to convert.

Return Value:
A Boolean value, that is, true or false.
Remarks
Valid strings are "1" or "true" for true and "0" or "false" for false.

Return to top


Method: ToByte(
   string s
)
Summary
Converts the String to a Byte equivalent.
C# Syntax:
public static byte ToByte(
   string s
);
Parameters:

s

The string to convert.

Return Value:
A Byte equivalent of the string.

Return to top


Method: ToChar(
   string s
)
Summary
Converts the String to a Char equivalent.
C# Syntax:
public static char ToChar(
   string s
);
Parameters:

s

The string containing a single character to convert.

Return Value:
A Char representing the single character.
Exceptions
Exception Type Condition
ArgumentNullException The value of the s parameter is null.
FormatException The s parameter contains more than one character.

Return to top


Overloaded Method: ToDateTime(
   string s
)
Summary
Converts the String to a DateTime equivalent.
C# Syntax:
public static DateTime ToDateTime(
   string s
);
Parameters:

s

The string to convert.

Return Value:
A DateTime equivalent of the string.
Example
The following example uses XmlConvert.ToDouble and ToDateTime to read strongly typed data.
using System;
using System.IO;
using System.Xml;

public class Sample
{

  public static void Main()
  {
    XmlTextReader reader = new XmlTextReader("orderData.xml");

    //Parse the file and pull out the order date and price.
    while (reader.Read()){
       if (reader.NodeType==XmlNodeType.Element){
         switch(reader.Name){
           case "order":
             DateTime orderDate = XmlConvert.ToDateTime(reader.GetAttribute("date"));
             Console.WriteLine("order date: {0}", orderDate.ToString());
             break;
           case "price":
             Double price = XmlConvert.ToDouble(reader.ReadInnerXml());
             Console.WriteLine("price: {0}", price.ToString());
             break;
         }
       }
    }

    //Close the reader.
    reader.Close();  
  }
}

    
The example uses the file, orderData.xml, as input.
<order date="2001-05-03">
  <orderID>367A54</orderID>
  <custID>32632</custID>
  <price>19.95</price>
</order>

    

Return to top


Overloaded Method: ToDateTime(
   string s,
   string format
)
Summary
Converts the String to a DateTime equivalent.
C# Syntax:
public static DateTime ToDateTime(
   string s,
   string format
);
Parameters:

s

The string to convert.

format

The format structure to apply to the converted DateTime. Valid formats include "yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets. The string is validated against this format.

Return Value:
A DateTime equivalent of the string.

Return to top


Overloaded Method: ToDateTime(
   string s,
   string[] formats
)
Summary
Converts the String to a DateTime equivalent.
C# Syntax:
public static DateTime ToDateTime(
   string s,
   string[] formats
);
Parameters:

s

The string to convert.

formats

An array containing the format structures to apply to the converted DateTime. Valid formats include "yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets.

Return Value:
A DateTime equivalent of the string.
Remarks
This method allows multiple formats for the string to be validated against.

Return to top


Method: ToDecimal(
   string s
)
Summary
Converts the String to a Decimal equivalent.
C# Syntax:
public static decimal ToDecimal(
   string s
);
Parameters:

s

The string to convert.

Return Value:
A Decimal equivalent of the string.

Return to top


Method: ToDouble(
   string s
)
Summary
Converts the String to a Double equivalent.
C# Syntax:
public static double ToDouble(
   string s
);
Parameters:

s

The string to convert.

Return Value:
A Double equivalent of the string.
Remarks
If s is INF or -INF, this method returns Double.PositiveInfinity or Double.NegativeInfinity respectively.
Example
The following example uses ToDouble and XmlConvert.ToDateTime to read strongly typed data.
using System;
using System.IO;
using System.Xml;

public class Sample
{

  public static void Main()
  {
    XmlTextReader reader = new XmlTextReader("orderData.xml");

    //Parse the file and pull out the order date and price.
    while (reader.Read()){
       if (reader.NodeType==XmlNodeType.Element){
         switch(reader.Name){
           case "order":
             DateTime orderDate = XmlConvert.ToDateTime(reader.GetAttribute("date"));
             Console.WriteLine("order date: {0}", orderDate.ToString());
             break;
           case "price":
             Double price = XmlConvert.ToDouble(reader.ReadInnerXml());
             Console.WriteLine("price: {0}", price.ToString());
             break;
         }
       }
    }

    //Close the reader.
    reader.Close();  
  }
}

    
The example uses the file, orderData.xml, as input.
<order date="2001-05-03">
  <orderID>367A54</orderID>
  <custID>32632</custID>
  <price>19.95</price>
</order>

    
See also:
Double.PositiveInfinity | Double.NegativeInfinity

Return to top


Method: ToGuid(
   string s
)
Summary
Converts the String to a Guid equivalent.
C# Syntax:
public static Guid ToGuid(
   string s
);
Parameters:

s

The string to convert.

Return Value:
A Guid equivalent of the string.

Return to top


Method: ToInt16(
   string s
)
Summary
Converts the String to a Int16 equivalent.
C# Syntax:
public static short ToInt16(
   string s
);
Parameters:

s

The string to convert.

Return Value:
An Int16 equivalent of the string.

Return to top


Method: ToInt32(
   string s
)
Summary
Converts the String to a Int32 equivalent.
C# Syntax:
public static int ToInt32(
   string s
);
Parameters:

s

The string to convert.

Return Value:
An Int32 equivalent of the string.

Return to top


Method: ToInt64(
   string s
)
Summary
Converts the String to a Int64 equivalent.
C# Syntax:
public static long ToInt64(
   string s
);
Parameters:

s

The string to convert.

Return Value:
An Int64 equivalent of the string.

Return to top


Method: ToSByte(
   string s
)
Summary
Converts the String to a SByte equivalent.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static sbyte ToSByte(
   string s
);
Parameters:

s

The string to convert.

Return Value:
An SByte equivalent of the string.

Return to top


Method: ToSingle(
   string s
)
Summary
Converts the String to a Single equivalent.
C# Syntax:
public static float ToSingle(
   string s
);
Parameters:

s

The string to convert.

Return Value:
A Single equivalent of the string.
Remarks
If s is INF or -INF, this method returns Single.PositiveInfinity or Single.NegativeInfinity respectively.
See also:
Single.PositiveInfinity | Single.NegativeInfinity

Return to top


Overloaded Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: ToString(
   bool value
)
Summary
Converts the Boolean to a String.
C# Syntax:
public static string ToString(
   bool value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the Boolean, that is, "true" or "false".

Return to top


Overloaded Method: ToString(
   byte value
)
Summary
Converts the Byte to a String.
C# Syntax:
public static string ToString(
   byte value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the Byte.

Return to top


Overloaded Method: ToString(
   char value
)
Summary
Converts the Char to a String.
C# Syntax:
public static string ToString(
   char value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the Char.

Return to top


Overloaded Method: ToString(
   DateTime value
)
Summary
Converts the DateTime to a String.
C# Syntax:
public static string ToString(
   DateTime value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the DateTime in the format yyyy-MM-ddTHH:mm:ss where 'T' is a constant literal.

Return to top


Overloaded Method: ToString(
   decimal value
)
Summary
Converts the Decimal to a String.
C# Syntax:
public static string ToString(
   decimal value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the Decimal.

Return to top


Overloaded Method: ToString(
   double value
)
Summary
Converts the Double to a String.
C# Syntax:
public static string ToString(
   double value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the Double.
Remarks
If value is Double.PositiveInfinity or Double.NegativeInfinity, this method returns the string INF or -INF respectively.
Example
The following example, converts data types to string and then writes the information out to the console.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    //Define the order data.  They will be converted to string 
    //before being written out.
    Int16 custID = 32632;
    String orderID = "367A54";
    DateTime orderDate = new DateTime();
    orderDate = DateTime.Now;
    Double price = 19.95;

    //Create a writer that outputs to the console.
    XmlTextWriter writer = new XmlTextWriter (Console.Out);
    writer.Formatting = Formatting.Indented;

    //Write an element (this one is the root)
    writer.WriteStartElement("order");

    //Write the order date.
    writer.WriteAttributeString("date", XmlConvert.ToString(orderDate, "yyyy-MM-dd"));

    //Write the order time.
    writer.WriteAttributeString("time", XmlConvert.ToString(orderDate, "HH:mm:ss"));
    
    //Write the order data.
    writer.WriteElementString("orderID", orderID);
    writer.WriteElementString("custID", XmlConvert.ToString(custID));
    writer.WriteElementString("price", XmlConvert.ToString(price));

    //Write the close tag for the root element
    writer.WriteEndElement();
             
    //Write the XML and close the writer
    writer.Close();  

  }

}

    
See also:
Double.PositiveInfinity | Double.NegativeInfinity

Return to top


Overloaded Method: ToString(
   Guid value
)
Summary
Converts the Guid to a String.
C# Syntax:
public static string ToString(
   Guid value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the Guid.

Return to top


Overloaded Method: ToString(
   short value
)
Summary
Converts the Int16 to a String.
C# Syntax:
public static string ToString(
   short value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the Int16.
Example
The following example, converts data types to string and then writes the information out to the console.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    //Define the order data.  They will be converted to string 
    //before being written out.
    Int16 custID = 32632;
    String orderID = "367A54";
    DateTime orderDate = new DateTime();
    orderDate = DateTime.Now;
    Double price = 19.95;

    //Create a writer that outputs to the console.
    XmlTextWriter writer = new XmlTextWriter (Console.Out);
    writer.Formatting = Formatting.Indented;

    //Write an element (this one is the root)
    writer.WriteStartElement("order");

    //Write the order date.
    writer.WriteAttributeString("date", XmlConvert.ToString(orderDate, "yyyy-MM-dd"));

    //Write the order time.
    writer.WriteAttributeString("time", XmlConvert.ToString(orderDate, "HH:mm:ss"));
    
    //Write the order data.
    writer.WriteElementString("orderID", orderID);
    writer.WriteElementString("custID", XmlConvert.ToString(custID));
    writer.WriteElementString("price", XmlConvert.ToString(price));

    //Write the close tag for the root element
    writer.WriteEndElement();
             
    //Write the XML and close the writer
    writer.Close();  

  }

}

    

Return to top


Overloaded Method: ToString(
   int value
)
Summary
Converts the Int32 to a String.
C# Syntax:
public static string ToString(
   int value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the Int32.

Return to top


Overloaded Method: ToString(
   long value
)
Summary
Converts the Int64 to a String.
C# Syntax:
public static string ToString(
   long value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the Int64.

Return to top


Overloaded Method: ToString(
   sbyte value
)
Summary
Converts the SByte to a String.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static string ToString(
   sbyte value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the SByte.

Return to top


Overloaded Method: ToString(
   float value
)
Summary
Converts the Single to a String.
C# Syntax:
public static string ToString(
   float value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the Single.
Remarks
If value is Single.PositiveInfinity or Single.NegativeInfinity, this method returns the string INF or -INF respectively.
See also:
Single.PositiveInfinity | Single.NegativeInfinity

Return to top


Overloaded Method: ToString(
   TimeSpan value
)
Summary
Converts the TimeSpan to a String.
C# Syntax:
public static string ToString(
   TimeSpan value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the TimeSpan.

Return to top


Overloaded Method: ToString(
   ushort value
)
Summary
Converts the UInt16 to a String.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static string ToString(
   ushort value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the UInt16.

Return to top


Overloaded Method: ToString(
   uint value
)
Summary
Converts the UInt32 to a String.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static string ToString(
   uint value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the UInt32.

Return to top


Overloaded Method: ToString(
   ulong value
)
Summary
Converts the UInt64 to a String.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static string ToString(
   ulong value
);
Parameters:

value

The value to convert.

Return Value:
A string representation of the UInt64.

Return to top


Overloaded Method: ToString(
   DateTime value,
   string format
)
Summary
Converts the DateTime to a String.
C# Syntax:
public static string ToString(
   DateTime value,
   string format
);
Parameters:

value

The value to convert.

format

The format structure that defines how to display the converted string. Valid formats include "yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets.

Return Value:
A string representation of the DateTime in the specified format.
Example
The following example, converts data types to string and then writes the information out to the console.
using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {

    //Define the order data.  They will be converted to string 
    //before being written out.
    Int16 custID = 32632;
    String orderID = "367A54";
    DateTime orderDate = new DateTime();
    orderDate = DateTime.Now;
    Double price = 19.95;

    //Create a writer that outputs to the console.
    XmlTextWriter writer = new XmlTextWriter (Console.Out);
    writer.Formatting = Formatting.Indented;

    //Write an element (this one is the root)
    writer.WriteStartElement("order");

    //Write the order date.
    writer.WriteAttributeString("date", XmlConvert.ToString(orderDate, "yyyy-MM-dd"));

    //Write the order time.
    writer.WriteAttributeString("time", XmlConvert.ToString(orderDate, "HH:mm:ss"));
    
    //Write the order data.
    writer.WriteElementString("orderID", orderID);
    writer.WriteElementString("custID", XmlConvert.ToString(custID));
    writer.WriteElementString("price", XmlConvert.ToString(price));

    //Write the close tag for the root element
    writer.WriteEndElement();
             
    //Write the XML and close the writer
    writer.Close();  

  }

}

    

Return to top


Method: ToTimeSpan(
   string s
)
Summary
Converts the String to a TimeSpan equivalent.
C# Syntax:
public static TimeSpan ToTimeSpan(
   string s
);
Parameters:

s

The string to convert.

Return Value:
A TimeSpan equivalent of the string.

Return to top


Method: ToUInt16(
   string s
)
Summary
Converts the String to a UInt16 equivalent.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static ushort ToUInt16(
   string s
);
Parameters:

s

The string to convert.

Return Value:
A UInt16 equivalent of the string.

Return to top


Method: ToUInt32(
   string s
)
Summary
Converts the String to a UInt32 equivalent.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static uint ToUInt32(
   string s
);
Parameters:

s

The string to convert.

Return Value:
A UInt32 equivalent of the string.

Return to top


Method: ToUInt64(
   string s
)
Summary
Converts the String to a UInt64 equivalent.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
public static ulong ToUInt64(
   string s
);
Parameters:

s

The string to convert.

Return Value:
A UInt64 equivalent of the string.

Return to top


Method: VerifyName(
   string name
)
Summary
Verifies that the name is a valid name according to the W3C Extended Markup Language recommendation.
C# Syntax:
public static string VerifyName(
   string name
);
Parameters:

name

The name to verify.

Return Value:
The name, if it is a valid XML name.
Exceptions
Exception Type Condition
XmlException The name is not a valid XML name.
Remarks
This method can be used with the XmlWriter class in the following manner.
              try{
                
                writer.WriteStartElement(XmlConvert.VerifyName("item"),"bar");
              }
              catch(Exception e)
              {
                Console.WriteLine("error");
              }
            
Example
The following example uses the VerifyName method to write an element name.
using System;
using System.Xml;

public class Sample{

  public static void Main(){
     XmlTextWriter writer = new XmlTextWriter ("out.xml", null);
     string tag = "item name";
   
  try{
	
     // Write the root element.
     writer.WriteStartElement("root");

     writer.WriteStartElement(XmlConvert.VerifyName(tag));
             
     }
     catch (Exception e){
        Console.WriteLine(e.Message);
        Console.WriteLine("Convert to a valid name...");
        writer.WriteStartElement(XmlConvert.EncodeName(tag));
     }

     writer.WriteString("hammer");
     writer.WriteEndElement();

     // Write the end tag for the root element.
     writer.WriteEndElement();
 
     writer.Close();
 
  }

}

    
See also:
XmlConvert.VerifyNCName

Return to top


Method: VerifyNCName(
   string name
)
Summary
Verifies that the name is a valid NCName according to the W3C Extended Markup Language recommendation.
C# Syntax:
public static string VerifyNCName(
   string name
);
Parameters:

name

The name to verify.

Return Value:
The name, if it is a valid NCName.
Remarks
An NCName is a name that cannot contain a colon.
See also:
XmlConvert.VerifyName

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.