如何把HTML code成的.resx资源文件? [英] How to put HTML code into a .resx resource file?

查看:122
本文介绍了如何把HTML code成的.resx资源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪,没有人问过这个....

我对4种语言创建模板HTML电子邮件。我希望把HTML模板到我的.resx文件所具有的code到他们很容易化,国际化的访问。像这样:

.resx文件:

 <数据NAME =BodyTemplateXML:空间=preserve>
    < VALUE>< D​​OCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01过渡// ENhttp://www.w3.org/TR/html4/loose.dtd\">!;
        < HTML和GT;
        < HEAD>
            <元CONTENT =text / html的;字符集= ISO-8859-1HTTP的当量=Content-Type的>
            <标题>标题< /标题>
            ...
        < /身体GT;
        < / HTML>
    < /值>
< /数据>

但是,很明显,编译器抱怨.resx文件内的HTML。 有没有.resx文件使用HTML(或XML的话)的方式。怎么样?

我使用的.NET版本4和dotLiquid作为模板引擎,如果该事项。


解决方案

建议:创建你想要的文件,将其命名为你想要的方式,例如my_template.html
然后将此文件添加到您的项目。

点击它,然后在属性窗口中的生成操作中选择并设置为嵌入的资源。

每当你想访问这个文件,你可以使用这样的事情(使用块的没有适当的:

 公共静态字符串ReadTextResourceFromAssembly(字符串名称)
    {
        使用(VAR流= Assembly.GetExecutingAssembly()。GetManifestResourceStream(名))
        {
            返回新的StreamReader(流).ReadToEnd();
        }
    }

它符合您的需要。上面的方法获取资源,说你已经把你的资源在项目 MyProject的在一个子目录HtmlTemplates并称之为my_template.html,然后就可以通过访问名字
MyProject.HtmlTemplates.my_template.html

您可以将其写出来,以文件或直接使用它,等等。

这有一些主要好处:你看你的html文件的您的项目,它具有HTML扩展,因此它的编辑在Visual Studio中有语法高亮和所有的工具应用到.html文件<。 / p>

我有一大堆的这些方法对我的单元测试,这一个数据中提取一个文件:

 公共静态无效WriteResourceToFile(字符串名称,字符串目的地)
    {
        使用(VAR流= Assembly.GetExecutingAssembly()。GetManifestResourceStream(名))
        {
            如果(流== NULL)
            {
                抛出新的ArgumentException(的String.Format(资源{0}找不到,名称),姓名);
            }            使用(VAR FS =新的FileStream(目的地,FileMode.Create))
            {
                stream.CopyTo(FS);
            }
        }
    }

Strange, that no one asked this before....

I am creating templated HTML emails for 4 languages. I want to put the HTML templates into my .resx files to have easy, internationalized access to them from code. Like so:

.resx file:

<data name="BodyTemplate" xml:space="preserve">
    <value><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
            <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
            <title>Title</title>
            ...
        </body>
        </html>
    </value>
</data>

But, obviously, the compiler complains about the HTML inside the .resx file. Is there a way to use HTML (or XML at all) in .resx files. How?

I am using .NET version 4 and dotLiquid as templating Engine, if that matters.

解决方案

Suggestion: Create the file you want, name it the way you want, e.g "my_template.html" then add this file to your project.

Click on it, then select in the properties window "Build Action" and set it to "Embedded Resource".

Whenever you want to access this file, you can use something like this (no with proper using block:

    public static string ReadTextResourceFromAssembly(string name)
    {
        using ( var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( name ) )
        {
            return new StreamReader( stream ).ReadToEnd();
        }
    }

Tailor it to your needs. The method above obtains the resource, say you have put your resource in your project MyProject in a subdirectory "HtmlTemplates and called it "my_template.html", then you can access it by the name MyProject.HtmlTemplates.my_template.html

You can then write it out to file or use it directly, etc.

This has some major benefits: You see your html file in your project, it has the html extension, so editing it in Visual Studio has syntax highlighting and all tools applied to .html files.

I have a bunch of those methods for my unit tests, this one extracts the data to a file:

    public static void WriteResourceToFile(string name, string destination)
    {
        using ( var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( name ) )
        {
            if ( stream == null )
            {
                throw new ArgumentException( string.Format( "Resource '{0}' not found", name), "name" );
            }

            using ( var fs = new FileStream( destination, FileMode.Create ) )
            {
                stream.CopyTo( fs );
            }
        }
    }

这篇关于如何把HTML code成的.resx资源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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