Access an XSLT file as resource from same project?

后端 未结 2 1530
难免孤独
难免孤独 2021-01-12 17:30

I have an XSLT file that I want to load and use to transform an XML file. I\'ve added the file to the same project as the code that uses it and put it in the \"Resources\" f

相关标签:
2条回答
  • 2021-01-12 17:36

    A co-worker has helped me find a solution. We added the resource via the properties of the project, so that I can access its content easily and used the following code.

    using (var reader = new StringReader(Resources.OrderOverview)) {
      using (XmlReader xmlReader = XmlReader.Create(reader)) {
        myXslTransform.Load(xmlReader);
        myXslTransform.Transform(fileName, arguments, xmlTextWriter);
      }
    }
    

    This is very similar to what outcoldman suggested with the subtle difference that the resource is accessed differently.

    0 讨论(0)
  • 2021-01-12 17:37

    Change the build action from Resource to Embedded Resource, after this you can do something like

    XslCompiledTransform myXslTransform = new XslCompiledTransform();
    
    var assembly = typeof(SomeTypeFromAssemblyWithResource).Assembly;
    using (var stream = assembly.GetManifestResourceStream("Resources.OrderManagement.OrderOverview.xslt"))
    {
        using (var xmlReader = XmlReader.Create(stream))
        {
            myXslTransform.Load(xmlReader );
        }
    }
    

    Resource name in dll can be tricky so maybe you want first to know the resource name with Assembly.GetManifestResourceNames. Compiler generates name based on the folder and assembly.

    0 讨论(0)
提交回复
热议问题