Embedding XML in Assembly using CodeDOM

只愿长相守 提交于 2019-12-13 21:07:01

问题


I have an XML generated at runtime. I need to embed this XML content into an assembly using CodeDOM. The XML will be accessed later from the assembly.

How can I embed XML into assembly ? Should I include the XML as EmbeddedResources in the assembly ?

Thanks


回答1:


Yes, for example, with the EmbeddedResources property. For example:

    Assembly a1 = typeof(MyClass).Assembly;
    System.CodeDom.Compiler.CompilerParameters cp = new System.CodeDom.Compiler.CompilerParameters();
    cp.ReferencedAssemblies.Add(a1.Location); // for example
    cp.GenerateInMemory = false;
    cp.GenerateExecutable = true;
    cp.IncludeDebugInformation = false;
    cp.CompilerOptions = "";
    cp.CompilerOptions += String.Format("/win32icon:\"{0}\"", nameOfIconFile);
    cp.CompilerOptions += " /target:winexe";
    cp.EmbeddedResources.Add(xmlFileName);

    var csharp = new Microsoft.CSharp.CSharpCodeProvider();
    System.CodeDom.Compiler.CompilerResults cr = csharp.CompileAssemblyFromSource(cp, LiteralSource);

The xml needs to be available in a file, in order to embed it as a resource.



来源:https://stackoverflow.com/questions/5920960/embedding-xml-in-assembly-using-codedom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!