How to convert value type as object to string using XmlConvert?

I am looking for something that is equivalent to the code below, but for any type of value without having to code the switch statement for each data type. The code below does not compile because XmlConvert.ToString () does not have an overload, which also accepts an object.

        int intValue = 10;
        object boxedValue = (object)intValue;
        string xmlValue = XmlConvert.ToString(boxedValue);

In other words, is there a better way:

public static string ToXmlString(Type type, object value) {

        switch(Type.GetTypeCode(type)) {
            case TypeCode.Int32:
                return XmlConvert.ToString((int) value);
            case TypeCode.DateTime:
                return XmlConvert.ToString((DateTime) value, XmlDateTimeSerializationMode.Unspecified);
            case TypeCode.Boolean:
                return XmlConvert.ToString((bool) value);

            // TODO:  Add case for all other value types!

            default:
                return value.ToString();
        }
    }
+5
source share
4 answers

All value types are inherently serializable. Therefore, you just need to use the XMLSerializer. Something like this would do this (based on your method):

public static string ToXmlString(Type type, object value)
{
    StringBuilder sb = new StringBuilder();
    System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sb);
    System.Xml.Serialization.XmlSerializer serial = 
        new System.Xml.Serialization.XmlSerializer(type);
    serial.Serialize(writer, value);
}
+2
source

Reflector , System.Xml.Linq.XContainer.GetStringValue( )

+1

( ) , ( , ). deserialize, , , , .

, , , , , . - , , E - XmlNode, , newVar - T, . - T ( /cast ) , / - ( ):

MethodInfo convertMethod;

if( Type.GetTypeCode(type) != TypeCode.String )
    convertMethod = typeof(XmlConvert).GetMethod ( "To" + type.Name );
else
    convertMethod = E.InnerText.GetType().GetMethod("Clone");

if( convertMethod == null )
{
    //Error
}
else
{
    if( Type.GetTypeCode (type)!= TypeCode.String )
        newVar = (T)convertMethod.Invoke( null, new object[] { E.InnerText } );
    else
        newVar = (T)convertMethod.Invoke ( E.InnerText, new object[]{} );
}

( , ToString() - ).

( ) - ( , , ):

MethodInfo convertMethod;

if( Type.GetTypeCode(type) != TypeCode.String )
    convertMethod = typeof(XmlConvert).GetMethod ( "ToString", new Type[] {typeof(T)} );
else
    convertMethod = typeof(string).GetMethod("Clone");

if( convertMethod == null )
{
    //Error
}
else
{
    string str;
    if( Type.GetTypeCode (type)!= TypeCode.String )
        str = (string)convertMethod.Invoke( null, new object[] { Value } );
    else
        str = (string)convertMethod.Invoke ( Value, new object[]{} );
}

T , Value T, . , ToString, T. .

+1

, , - XML?

, XmlSerialization. - .Net XML:).

Also, is there a reason you box your value? XmlConvert.ToString () has 19 overloads, many of which accept primitives.

0
source

All Articles