XmlSerializer , base64 编码一个 String 成员 [英] XmlSerializer , base64 encode a String member

查看:28
本文介绍了XmlSerializer , base64 编码一个 String 成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个简单的案例

public class Test {
  public String myString;
}

有什么方法可以告诉 XmlSerializer 在序列化 myString 时对其进行 base64 编码吗?

Is there any way I can tell XmlSerializer to base64 encode myString when serializing it ?

推荐答案

您可以简单地将其设置为 byte[] 属性,它会自动对其进行 Base64 编码:

You can simply set it to be a byte[] property and it will Base64 encode it automatically:

public class Test {
  public byte[] MyProperty {get;set;}

  public void SetMyProperty(string text)
  {
      MyProperty = System.Text.Encoding.Unicode.GetBytes(text);
  }
}

Test test = new Test();
test. SetMyProperty("123456789123456789");

输出:

<MyProperty>MQAyADMANAA1ADYANwA4ADkAMQAyADMANAA1ADYANwA4ADkA</MyProperty>

(尝试解码此处)

不幸的是,没有办法(据我所知)将 MyProperty 设为私有并且仍然在 System.Xml.Serialization 中被序列化.

Unfortunately there is no way (that I know of) to make MyProperty private and still be serialized in System.Xml.Serialization.

这篇关于XmlSerializer , base64 编码一个 String 成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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