如何隐藏XML文件内容 [英] How to hide XML files content

查看:859
本文介绍了如何隐藏XML文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的,我想从客户端隐藏的XML文件,我想成为只可用于应用程序。我读写从/给他们使用的XmlSerializer 。如何才能做到这一点?我读到嵌入的资源,但是从我所看到的,我需要读取和写入使用某种流的文件。我想知道是否有另一种方法,它可以让我用从客户端访问它们的XmlSerializer 和隐藏它们。

I have a series of XML files which I want to hide from the client and I want to be available only for the application. I read and write from/to them using XmlSerializer. How can this be done? I read about embedded resources, but from what I've seen, I need to read and write to the files using some sort of stream. I was wondering if there is another approach which would allow me to access them using XmlSerializer and hide them from the client.

推荐答案

如果你只想隐藏它们(一种混淆以prevent的休闲的变化)你可以考虑COM preSS他们。例如在本C#的反序列化功能的例子:

If you just want to hide them (kind of obfuscation to prevent casual changes) you may consider to compress them. For example this an example of C# deserialization function:

static T Deserialize<T>(string path, object obj)
{
    var serializer = new XmlSerializer(typeof(T));

    using (var stream = new GZipStream(File.OpenRead(path),
                                       CompressionMode.Decompress))
    {
        return (T)serializer.Deserialize(stream);
    }
}

您的客户将看到的的文件,他们将不能够更改/检查它(而且它只是一个COM pressed流,使他们甚至不能解压缩它们)。为了清楚起见,这相当于序列功能:

Your customers will see a binary file and they won't be able to change/inspect it (moreover it's just a compressed stream so they can't even unzip them). For clarity this is equivalent serialization function:

static void Serialize<T>(string path, T obj)
{
    var serializer = new XmlSerializer(typeof(T));

    using (var stream = new GZipStream(File.Create(path),
                                       CompressionMode.Compress))
    {
        serializer.Serialize(stream, obj);
    }
}

注意:在你原来的问题,你没有说你的环境(?.NET Java的)什么,我提供了code假设你的编程在C#中,但你可以申请同样的技术与你使用任何其他语言/环境。

Note: in your original question you didn't say anything about your environment (.NET? Java?), I provided code assuming you're programming in C# but you can apply same technique with any other language/environment you're using.

更新这是一个小的测试程序,看看它是如何工作的:

Update this is a small test program to see how it works:

public class Test
{
    public string Name { get; set; }
    public string Value { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Serialize(@"c:\test.dat", new Test { Name = "A", Value = "B" });
    }

    // Place here Serialization<T>() method
}

这篇关于如何隐藏XML文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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