2010-11-04 119 views
0

我已經給了一個任務,爲我們的網站的靜態內容製作一個資源xml文件,以便我們可以查找該xml文件中針對特定id的內容並顯示它。例如,如果我們寫我們的主頁上「歡迎來到xyz.com」應該存儲在XML文件中像資源xml文件

<word> 
    <add Key="welcome" value="Welcome to xyz.com" /> 
</word> 

<word> 
    <add key="key1" value="some other static content" /> 
</word> 

,所以我們將能夠顯示由ID =「歡迎」的文本.. plz幫助。

訪問資源文件

string key = "Home"; 
      string resourceValue = string.Empty; 
      string resourceFile = "Resource";//name of my resource file Resource.resx 

      string filePath =System.AppDomain.CurrentDomain.BaseDirectory.ToString(); 

      ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null); 


      resourceValue = resourceManager.GetString(key); 

,並收到以下錯誤

Could not find any resources appropriate for the specified culture (or the neutral culture) on disk. 
baseName: Resource locationInfo: <null> fileName: Resource.resources 
+0

你還沒有真正問過一個具體的問題。你有什麼麻煩? – 2010-11-04 07:12:07

+1

研究在Visual Studio中使用資源(resx)文件。 – 2010-11-04 08:19:28

+0

我不知道該怎麼做,我認爲生病必須使用XML讀取器,但具體到底是什麼? – Rafay 2010-11-04 09:31:40

回答

2

嘗試像這樣的XML格式:

<Page Id="Home"> 
    <Elements> 
     <Element Id="welcome"> 
      <Value>Welcome to xyz.com</Value> 
     </Element> 

     <Element Id="key1"> 
      <Value>some other static content</Value> 
     </Element>  
    </Elements> 
</Page> 

爲什麼?我的第一個想法是每個頁面都需要它自己的ID。所以我用兩個元素顯示了一個名爲「Home」的頁面。也可以使用標籤而非屬性,如果您有需要轉義的字符,可以使用CDATA部分[http://en.wikipedia.org/wiki/CDATA]。否則,你將不得不轉換<,>,&,那會變得很難看。

<Element Id="key1"> 
    <Value><![CDATA[some other static content with < > in it!]]></Value> 
</Element> 

或者如果你想包括一個div?上課?

<Element Id="key1"> 
    <Value><![CDATA[<div class="wrapper">some other static content with in it!</div>]]></Value> 
</Element> 

它更好。

通常,我使用的標籤不僅僅是屬性。使用XPath查找標籤更容易。

+0

tnx安東尼,我會嘗試你的溶膠,希望它的工作原理 – Rafay 2010-11-06 05:33:50