2011-05-07 61 views
0

我有在運行時生成的XML。我需要使用CodeDOM將此XML內容嵌入到程序集中。 XML將在稍後從程序集中訪問。使用CodeDOM在Assembly中嵌入XML

如何將XML嵌入到程序集中?我是否應該將XML作爲EmbeddedResources包含在程序集中?

感謝

+0

是的,這應該工作得很好。 – Cheeso 2011-05-07 12:31:22

回答

0

是,例如,與EmbeddedResources財產。例如:

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); 

的XML必須提供文件,以嵌入它作爲一種資源。

+0

如何從代碼中訪問XML或任何資源? 「my.resources」關鍵字不可用於codedom(?) – MilMike 2011-07-29 07:03:50

+0

格式字符串應該可能是'「\」/ win32icon:{0} \「」',即引號應該跨越整個參數 – 2012-08-06 05:43:23