Warm tip: This article is reproduced from serverfault.com, please click

c#-XSLT从文件读取

(c# - XSLT Reading from File)

发布于 2020-12-02 14:42:52

我有一个代码可以从参考资料中读取Xslt模板,但是我必须使其从应用程序根目录中的单独文件中读取。这是我的代码:

            docs[0] = new DocumentReferenceType
            {
                ID = new IDType { Value = new Guid().ToString() },
                IssueDate = new IssueDateType { Value = DateTime.Now },
                DocumentType = new DocumentTypeType { Value = "xslt" },
                Attachment = new AttachmentType
                {
                    EmbeddedDocumentBinaryObject = new EmbeddedDocumentBinaryObjectType
                    {
                        filename = "customxslt.xslt",
                        encodingCode = "Base64",
                        mimeCode = "applicationxml",
                        format = "",
                        characterSetCode = "UTF-8",
                        Value = Encoding.UTF8.GetBytes(Properties.Resource1.XSLTFile)

                    }
                }
            };
            return docs;

我应该写什么从根目录中的XSLTFile.xslt读取的“ Properties.Resource1.XSLTFile”?我试着这样写:

        string _filePath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);
            _filePath += @"\XSLTFile.xslt";

而且我在那里用“ _filePath”更改了“ Properties.Resource1.XSLTFile”,但它给了我这个错误:“ HTML转换失败:XSLT编译错误”

Questioner
Tuğhan Avcı
Viewed
11
Tuğhan Avcı 2020-12-03 16:06:00

我得到了帮助,我正在分享,也许会有人需要它

            string _filePath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);
            _filePath += @"\xslt\earsiv.xslt";
            string xs = System.IO.File.ReadAllText(_filePath);

            docs[0] = new DocumentReferenceType
            {
                ID = new IDType { Value = new Guid().ToString() },
                IssueDate = new IssueDateType { Value = DateTime.Now },
                DocumentType = new DocumentTypeType { Value = "xslt" },
                Attachment = new AttachmentType
                {
                    EmbeddedDocumentBinaryObject = new EmbeddedDocumentBinaryObjectType
                    {
                        filename = "customxslt.xslt",
                        encodingCode = "Base64",
                        mimeCode = "applicationxml",
                        format = "",
                        characterSetCode = "UTF-8",
                        Value = Encoding.UTF8.GetBytes(xs)
                    }
                }
            };
            return docs;