应用SoapIgnore属性不采取任何影响系列化结果 [英] Applying SoapIgnore attribute doesn't take any effect to serialization result

查看:145
本文介绍了应用SoapIgnore属性不采取任何影响系列化结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚.NET序列化的东西,遇到了问题。我做了一个简单的程序测试,并得到了坚持使用属性。这里的code:

I'm trying to figure out .NET serialization stuff and experiencing a problem. I've made a simple program to test it and got stuck with using attributes. Here's the code:

[Serializable]
public class SampleClass
{
    [SoapIgnore]
    public Guid InstanceId
    {
        get;
        set;
    }
}

class Program
{
    static void Main()
    {

        SampleClass cl = new SampleClass { InstanceId = Guid.NewGuid() };
        SoapFormatter fm = new SoapFormatter();
        using (FileStream stream = new FileStream(string.Format("C:\\Temp\\{0}.inv", Guid.NewGuid().ToString().Replace("-", "")), FileMode.Create))
        {
            fm.Serialize(stream, cl);
        }
    }
}

的问题是,不忽略INSTANCEID而序列完成。我得到.inv文件是这样的:

The problem is that InstanceId is not ignored while serialization is done. What I get in .inv file is something like this:

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:SampleClass id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/TestConsoleApp/TestConsoleApp%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<_x003C_InstanceId_x003E_k__BackingField>
<_a>769807168</_a>
<_b>27055</_b>
<_c>16408</_c>
<_d>141</_d>
<_e>210</_e>
<_f>171</_f>
<_g>30</_g>
<_h>252</_h>
<_i>196</_i>
<_j>246</_j>
<_k>159</_k>
</_x003C_InstanceId_x003E_k__BackingField>
</a1:SampleClass>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我可以从文档InstanceID属性理解是不应该被序列化,因为SoapIgnore属性适用于它。我失去了一些东西?

As I can understand from documentation InstanceId property is not supposed to be serialized since SoapIgnore attribute is applied to it. Am I missing something?

推荐答案

的属性为标准的Web服务的XML序列化。 SoapFormatter是远程处理序列化不适合Web服务序列化。

The attributes are for standard Web Service XML serialization. SoapFormatter is for Remoting serialization not for Web Service serialization.

为了让你的例子工作,你应该为SOAP-CN codeD的消息部分提供类型映射。

To make you example work you should provide type mappings for SOAP-encoded message parts.

[Serializable]
public class SampleClass
{

    [SoapIgnore]
    public Guid InstanceId
    {
        get;
        set;
    }
}

class Program
{
    static void Main()
    {

        SampleClass cl = new SampleClass { InstanceId = Guid.NewGuid() };
        XmlTypeMapping xtm = new SoapReflectionImporter().ImportTypeMapping(typeof(SampleClass));
        XmlSerializer xs = new XmlSerializer(xtm);

        using (FileStream stream = new FileStream(string.Format("C:\\Temp\\{0}.inv", Guid.NewGuid().ToString().Replace("-", "")), FileMode.Create))
        {
            xs.Serialize(stream, cl);
        }
    }
}

这篇关于应用SoapIgnore属性不采取任何影响系列化结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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