SoapFormatter序列化问题

SoapFormatter序列化问题

本文介绍了SoapFormatter序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我需要一些帮助。如果有人可以帮助我,那就太好了。

Hello all, i need a bit of help. It would be great if someone can help me.

这是我的代码


[Serializable()]

class A : System.Runtime.Serialization.IDeserializationCallback

{

[System.Xml.Serialization.SoapIgnore()]

public decimal price;

public int quantity;

[NonSerialized()]

private decimal _total;

private decimal Total

{

get { return _total; }

}

public void SetTotal()

{ _total = price * quantity; }

public void PrintInfo()

{

Console.WriteLine("Price is " + price);

Console.WriteLine("Quantity is " + quantity);

Console.WriteLine("Total is " + _total);

Console.WriteLine("-------------------------------");

}

}



A obj = new A();

obj.price = 10;

obj.quantity = 50;

obj.SetTotal();

obj.PrintInfo();

--test code

using (System.IO.FileStream fs = new FileStream(@"C:\Documents and Settings\Edi\Desktop\Edi.xml", FileMode.Create, FileAccess.Write, FileShare.Read))

{

System.Runtime.Serialization.Formatters.Soap.SoapFormatter bf = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();

bf.Serialize(fs, obj);

}


推荐答案

SOAPFormatter不会考虑SOAP Ignore。您需要使用如下的XMLSerlizer。

SOAP Ignore will not be considered by SOAPFormatter. You need to use the XMLSerlizer as below.

 

请参阅http://msdn.microsoft.com/en-us/library /system.xml.serialization.soapattributes.soapignore.aspx#Y342

Refer to http://msdn.microsoft.com/en-us/library/system.xml.serialization.soapattributes.soapignore.aspx#Y342

 

 

 // Create an instance of the XmlSerializer class.
      XmlTypeMapping myMapping =
      (new SoapReflectionImporter().ImportTypeMapping(
      typeof(Group)));
      XmlSerializer mySerializer =
      new XmlSerializer(myMapping);


这篇关于SoapFormatter序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:07