在我的项目中,我有xml文件来存储和检索Device
个对象
我希望用户只能通过我的应用程序在我的xml文件中添加或删除,而不是手动
所以我需要一些格式的故事数据用户无法理解它像asp.net base64
中的viewState或其他东西
我的问题是我如何在xml文件中保存我的数据,但我希望用户无法理解所写的内容
这是我的设备类
public class Device
{
public string Username { get; set; }
public string AgentName { get; set; }
public string Password { get; set; }
public string Domain { get; set; }
public string PeerURI { get; set; }
public string SipURI { get; set; }
public string FQDN { get; set; }
public Enums.DeviceType Type { get; set; }
public Enums.ServerTransportType TransportType { get; set; }
public bool IsInitialized { get; set; }
}
这是我的xml文件
<?xml version="1.0" encoding="utf-8"?>
<settings>
<username>foo</username>
<AgentName>foo1</AgentName>
<password>foo2</password>
<domain>go</domain>
<peerUri>140.242.250.200</peerUri>
<sipUri>sip:xxx@xxxxx.com</sipUri>
<fqdn>ff.go.xxxx.com</fqdn>
<type>2</type>
<transportType>2</transportType>
</settings>
答案 0 :(得分:2)
Base64应该这样做。
String username = "foo";
byte[] EncodeAsBase64 = System.Text.ASCIIEncoding.ASCII.GetBytes(username);
string enc_username = System.Convert.ToBase64String(EncodeAsBase64);
然后使用serializer
存储enc_username。
答案 1 :(得分:2)
这样的东西应该适用于Base64编码整个序列化过程...
using System.IO;
using System.Security.Cryptography;
using System.Xml.Serialization;
...
private void WriteToFile(Device device, string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
using (CryptoStream cs = new CryptoStream(fs, new ToBase64Transform(), CryptoStreamMode.Write))
{
XmlSerializer x = new XmlSerializer(typeof(Device));
x.Serialize(cs, device);
}
}
}
private Device ReadFromFile(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
using (CryptoStream cs = new CryptoStream(fs, new FromBase64Transform(), CryptoStreamMode.Read))
{
XmlSerializer x = new XmlSerializer(typeof(Device));
return x.Deserialize(cs) as Device;
}
}
}
答案 2 :(得分:1)
如果您不希望任何人使用文本编辑器编辑XML数据文件,请执行以下操作:
答案 3 :(得分:1)
如果使用二进制序列化,则该文件根本不是人类可读的。
以下是您的问题的简短示例:
public class Enums
{
public enum DeviceType
{
Mouse,
HardDisk,
CdRom,
}
public enum ServerTransportType
{
Udp,
Tcp,
}
}
[Serializable]
public class Device
{
public string Username { get; set; }
public string AgentName { get; set; }
public string Password { get; set; }
public string Domain { get; set; }
public string PeerURI { get; set; }
public string SipURI { get; set; }
public string FQDN { get; set; }
public Enums.DeviceType Type { get; set; }
public Enums.ServerTransportType TransportType { get; set; }
public bool IsInitialized { get; set; }
}
public class BinarySerialize
{
public void Test()
{
var device = new Device();
device.Username = "userName";
device.AgentName = "agentName";
device.Password = "password";
device.Domain = "domain";
device.PeerURI = "peerURI";
device.SipURI = "sipURI";
device.FQDN = "fqdn";
device.Type = Enums.DeviceType.HardDisk;
device.TransportType = Enums.ServerTransportType.Tcp;
device.IsInitialized = true;
string fileName = @"C:\temp\device.bin";
this.Serialize(device, fileName);
var d = this.Deserialize(fileName);
}
public void Serialize(Device device, string fileName)
{
using (Stream stream = File.Open(fileName, FileMode.Create))
{
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, device);
stream.Close();
}
}
public Device Deserialize(string fileName)
{
var device = new Device();
using (Stream stream = File.Open(fileName, FileMode.Open))
{
BinaryFormatter bformatter = new BinaryFormatter();
device = (Device)bformatter.Deserialize(stream);
stream.Close();
}
return device;
}