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

查看:23
本文介绍了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;
}

可怕的例子!我只是自相矛盾!

Horrible example! I just contradicted myself!

我不能做以下事情:

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 的开头,并希望能够从那里确定类型.这取决于 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);

并将反序列化方法更改为如下所示:

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);
}

然而,这只会给你一个 object...如果所有有问题的类型都实现了一个通用接口,你可以反序列化如上但将返回类型更改为接口(并强制转换为它在返回语句中)

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天全站免登陆