xml字符串的通用反序列化 [英] Generic deserialization of an xml string

查看:271
本文介绍了xml字符串的通用反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆不同的DTO类。它们被一次序列化为一个XML字符串,并被发送到Web应用程序的客户端。现在,当客户端回溯一个XML字符串,我需要反序列化回它的DTO类的一个实例,它表示。问题是,我想让它通用,可能是一个函数接受一个xml字符串和吐出一个类型的对象。像这样长的行:

I have a bunch of different DTO classes. They are being serialized into an XML string at one point and shot over to client-side of the web app. Now when the client shoots back an XML string, I need to deserialize it back to an instance of the DTO class that it represents. The problem is that I want to make it generic and possibly a function which takes in an xml string and spits out an object of a type. Something like a long these lines:

public sometype? Deserialize (string xml)
{
//some code here
return objectFromXml;
}

编辑:可怕的例子!

我无法执行以下操作:

Person person = Deserialize(personXmlStringFromClient);

,因为我不知道personXmlStringFromClient是Person DTO对象实例的表示。

because I don't know that personXmlStringFromClient is a representation of Person DTO object instance.

我不知道是什么序列化对象给我,这似乎是我的问题。我一直在阅读关于反射和其他技术,涉及将类型粘贴到xml中,以便解串器知道该怎么做。我似乎不能把它一起拉到一个工作。此外,在几乎大多数示例中,作者知道在反序列化之后将会有什么类型。欢迎任何建议!

I don't know what serialized object is given to me and that seems to be my problem here. I've been reading about reflection and other techniques which involve sticking the type into the xml so that deserializer knows what to do with it. I can't seem to pull it all together into one working piece. Also, in pretty much most examples, the author knows what type there will be after deserialization. Any suggestion is welcome! If I need to do something special with the serialization process, please share that too.

推荐答案

您可以使用泛型:

    public T Deserialize<T>(string input)
        where T : class
    {
        System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));

        using (StringReader sr = new StringReader(input))
            return (T)ser.Deserialize(sr);
    }

如果你不知道哪种类型,固定数量的可能类型,你可以尝试反序列化每个,直到你没有遇到异常。不太好,但它会工作。

If you don't know which type it will be, I assume you have a fixed number of possible types, and you could try deserializing to each one until you don't encounter an exception. Not great, but it would work.

或者,你可以检查xml的外部对象名称的开始,并希望能够从那里确定类型。

Or, you could inspect the start of the xml for the outer object name and hopefully be able to determine the type from there. This would vary depending on what the xml looks like.

编辑:每次编辑,如果调用者知道他们传递的类型,他们可以提供完全限定的类型名作为字符串作为服务的附加参数吗?

Per your edit, if the caller knows the type that they are passing, could they supply the fully qualified typename as a string as an additional parameter to the service?

如果是,您可以这样做:

If so, you could do this:

    Type t = Type.GetType(typeName);

并将Deserialize方法更改为:

and change the Deserialize method to be like this:

public object Deserialize(string input, Type toType)
{
    System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(toType);

    using (StringReader sr = new StringReader(input))
        return ser.Deserialize(sr);
}

但是,这只会得到一个 code> ...如果所有类型的问题实现一个公共接口,你可以反序列化如上,但更改的接口的返回类型(并转换到它的return语句)

However, this only gets you an object... If all of the types in question implement a common interface, you could deserialize as above but change the return type to the interface (and cast to it in the return statement)

这篇关于xml字符串的通用反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆