2011-04-08 67 views
1

我有一個silverlight程序集,它有幾個xml文件。所有這些文件都被嵌入爲資源。我也不知道文件名。我需要加載所有文件並在數據字典中構造數據。 我知道如何加載xml文件,如果我知道文件名。如下圖所示如何從silverlight程序集加載所有資源?

this.filePath = string.Format(@"/{0};component/internal/{1}", assemblyName, "TaxRules.xml"); 
StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(filePath, UriKind.Relative)); 
     Stream stream = streamResourceInfo.Stream; 
     //check for performance 
     XmlReader reader = XmlReader.Create(stream); 

我不知道如何加載所有的XML文件而無需知道文件名.. 有這方面的幫助是非常appreacited

回答

0

如果對XML文件的生成操作是嵌入式資源,你在裝配中,你可以從裝配中獲得它

eg

Assembly asm = Assembly.GetExecutingAssembly(); 

var names = (from n in asm.GetManifestResourceNames() 
         where n.EndsWith(".xml") 
         select n 
         ).ToList(); 

然後,當你想流使用GetManifestResourceStream

例如

Dictionary<string, System.IO.Stream> resources = new Dictionary<string,System.IO.Stream>(); 
    foreach (string name in names) 
    { 
      resources.Add(name, asm.GetManifestResourceStream(name)); 

    } 

注:用VS 2010 Silverlight工具包4

+0

測試謝謝。感謝你的幫助。如果它只是一個資源呢?資源和嵌入式資源有什麼區別?這些xml文件並不是相同的。 – 2011-04-08 16:33:53

+0

程序集中存在嵌入式資源。 (這是我的意思,雖然你的意思是「作爲資源嵌入」)一個資源,如果我正確理解它遵循[包URI的計劃](http://msdn.microsoft.com/en-us/library/aa970069.aspx) 。我不知道如何列舉這些資源。抱歉 – 2011-04-08 17:59:01