如何将以下c# 4.8框架迁移到.NET Core3.1?
守则是:
public enum E { V }
[Serializable]
public class Person
{
public E my_enum;
public bool my_bool;
}
[...]
Person p = new Person();
using (FileStream fs = new FileStream("myfile.soap", FileMode.Create))
{
SoapFormatter f = new SoapFormatter();
f.Serialize(fs, p);
fs.Close();
}请给我所期望的:
<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:Person id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/ConsoleApp/ConsoleApp%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<my_enum>V</my_enum>
<my_bool>false</my_bool>
</a1:Person>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>阅读后:
以及:
还包括:
我试过:
static void Main(string[] args)
{
Person p = new Person();
using (FileStream fs = new FileStream("myfile.soap", FileMode.Create))
{
XmlTypeMapping myTypeMapping = new SoapReflectionImporter()
.ImportTypeMapping(typeof(Person));
XmlSerializer mySerializer = new XmlSerializer(myTypeMapping);
mySerializer.Serialize(fs, p);
fs.Close();
}
}这给了我:
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1">
<my_enum xsi:type="E">V</my_enum>
<my_bool xsi:type="xsd:boolean">false</my_bool>
</Person>发布于 2022-10-14 08:59:31
这取决于您要修复的部分;如果是名称空间:告诉XmlSerializer关于它们的信息
// note: this would be [XmlType(...)] if it wasn't the root element
[XmlRoot(Namespace = "http://schemas.microsoft.com/clr/nsassem/ConsoleApp/ConsoleApp%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull")]
public class Person
{
[XmlElement(Namespace = "")]
public E my_enum;
[XmlElement(Namespace = "")]
public bool my_bool;
}如果是名称空间-别名(尽管这不会改变含义):
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("a1", "http://schemas.microsoft.com/clr/nsassem/ConsoleApp/ConsoleApp%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull");
XmlSerializer mySerializer = new XmlSerializer(typeof(Person));
mySerializer.Serialize(fs, p, ns);如果是包装节点:您需要添加这些节点,可能是通过类型添加,可能是手动修改。
但是,从根本上说,XmlSerializer和SoapFormatter并不是1:1可互换的,您不应该试图使它们成为可互换的。老实说,把它看作是一个基本的序列化开关,比尝试破解它要安全得多,直到它起作用为止。
https://stackoverflow.com/questions/74066641
复制相似问题