序列化这个有很多麻烦。在 XML 中序列化非常新
这是我写入序列化数据类的地方。
string addrID="Sante Fe, New Mexico";
INPUT r = new INPUT();
INPUTLOG ls = new INPUTLOG();
ls.id = id;
ls.password = password;
r.LOGIN = ls;
REQUESTaddr l = new REQUESTaddr();
l.loan_id = addrID;
INPUTLOGINREQ r = new INPUTLOGINREQ();
r.LOAN = l;
loan.REQUEST = r;
string serializedSearch = doSerializationsearch(r);
private static string doSerializationsearch(INPUT r)
{
string serializedValue = string.Empty;
XmlSerializer xmlSerializer = new XmlSerializer(r.GetType());
using (MemoryStream ms = new MemoryStream())
{
xmlSerializer.Serialize(ms, r);
ms.Position = 0;
var sr = new StreamReader(ms);
serializedValue = sr.ReadToEnd();
}
serializedValue = serializedValue.Replace("version=\"0\"", string.Empty).Replace("<?xml version=\"1.0\"?>", string.Empty).Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", string.Empty);
return serializedValue;
}
出于某种原因,它使用我不想要的 addridfield 元素将其序列化。
<INPUT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LOGIN api_user_id="user" api_password="password" />
<REQUEST>
<Address addr_id="53196d9f4c544e4da9f647b2c86444c2">
<addridField>53196d9f4c544e4da9f647b2c86444c2</addridField> <<<<<I do not want this
</Address>
</REQUEST>
</INPUT>
我没有注意到我的课程有任何问题,但其他人注意到我在添加这个额外元素时做错了。