Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deserialize xml string to object

Tags:

c#

i wrote this method in order to convert a xml string into the object:

private object Deserializer(Type type)
{
    object instance = null;
    try
    {
        XmlSerializer xmlSerializer = new XmlSerializer(type);
        using (StringReader stringreader = new StringReader(somestring))
        {
            instance = (type)xmlSerializer.Deserialize(stringreader);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return instance;
} 

but here:

instance = (type)xmlSerializer.Deserialize(stringreader);

this error shows up: The type or namespace name 'type' could not be found (are you missing a using directive or an assembly reference?) how can i fix it?

like image 768
Mohammad Avatar asked Aug 04 '15 07:08

Mohammad


People also ask

How to Deserialize string to XML object in c#?

string operationXML = webRequest. getJSON(args[1], args[2], pollURL); var serializer = new XmlSerializer(typeof(StatusDocumentItem)); StatusDocumentItem result; using (TextReader reader = new StringReader(operationXML)) { result = (StatusDocumentItem)serializer.

What is the correct way of using XML Deserialization?

XML Serialization Considerations Type identity and assembly information are not included. Only public properties and fields can be serialized. Properties must have public accessors (get and set methods). If you must serialize non-public data, use the DataContractSerializer class rather than XML serialization.

How to serialize XML string in c#?

Serialization is handled by System. Runtime. Serialization namespace. To serialize an object, you need to create two things, stream to contain the serialized objects and a formatter to serialize the objects into the stream.


2 Answers

You canno't cast to "type" you need to specify the exact type like this (for string):

(string)xmlSerializer.Deserialize(stringreader);

Maybe consider using generic function like this:

private T Deserializer<T>()
{
    T instance = null;
    try
    {
        var xmlSerializer = new XmlSerializer(typeof(T));
        using (var stringreader = new StringReader(somestring))
        {
            instance = (T)xmlSerializer.Deserialize(stringreader);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return instance;
} 

and than call the function like this:

var instance = xmlSerializer.Deserialize<SomeType>();

if you want to specify the type only in runtime you can use:

instance = Convert.ChangeType(xmlSerializer.Deserialize(stringreader), type);
like image 146
Guy Levin Avatar answered Nov 09 '22 06:11

Guy Levin


But what is a Type is class that u made or what ?

Maybe u want do to :

private object Deserializer<T>(T type)

Edit Try this :

private static T LoadData<T>() where T : new ()
    {
        using (var reader = new StreamReader(@_path))
        {
            var xmlSerializer = new XmlSerializer(typeof(T));
            return (T) xmlSerializer.Deserialize(reader);
        }

    }
like image 42
Aht Avatar answered Nov 09 '22 05:11

Aht