开发者

Embedding XML in Assembly using CodeDOM

开发者 https://www.devze.com 2023-03-03 08:16 出处:网络
I have an XML generated at runtime. I need to embed this XML content into an assembly using CodeDOM. The开发者_JAVA百科 XML will be accessed later from the assembly.

I have an XML generated at runtime. I need to embed this XML content into an assembly using CodeDOM. The开发者_JAVA百科 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消