相关疑难解决方法(0)

序列化为 XML 并包含序列化对象的类型

在上一个关于将对象序列化为XmlDocumentin C# 的问题中,我需要将一些错误信息序列化为XmlDocument从 asmx 样式的 web 服务调用返回的 。在客户端上,我需要将XmlDocument返回反序列化为一个对象。

如果您知道类型,这很简单,但我意识到我想要一种灵活的方法,其中要反序列化的类型也在XmlDocument. 我目前正在通过向具有类型名称的 中添加一个XmlNode来手动执行此操作XmlDocument,计算如下:

    Type type = fault.GetType();
    string assemblyName = type.Assembly.FullName;

    // Strip off the version and culture info
    assemblyName = assemblyName.Substring(0, assemblyName.IndexOf(",")).Trim();

    string typeName = type.FullName + ", " + assemblyName;
Run Code Online (Sandbox Code Playgroud)

然后在客户端上,我首先从 中取回此类型名称XmlDocument,并创建传递到XmlSerialiser因此的类型对象:

        object fault;
        XmlNode faultNode = e.Detail.FirstChild;
        XmlNode faultTypeNode = faultNode.NextSibling;

        // The typename of the fault type is the inner …
Run Code Online (Sandbox Code Playgroud)

c# xml-serialization

5
推荐指数
1
解决办法
3384
查看次数

标签 统计

c# ×1

xml-serialization ×1