2011-08-15 46 views
3

我有一個3D對象及其數據點。數據由1000個點組成。C#解決方案資源管理器中的XML文件

以下是數據點。

public static List<MyLine> OpenProject(string _file) 
    { 
     List<MyLine> Lines = new List<MyLine>(); 
     XmlDocument doc = new XmlDocument(); 
     doc.LoadXml(_file); 
     XmlNodeList coordinates = doc.SelectNodes("/Siene/MyLine/Coordinates"); 
     foreach (XmlNode coordinat in coordinates) 
     { 
      int x1 = Int32.Parse(coordinat["Start"].Attributes["X"].Value); 
      int y1 = Int32.Parse(coordinat["Start"].Attributes["Y"].Value); 
      int z1 = Int32.Parse(coordinat["Start"].Attributes["Z"].Value); 
      MyLine czg = new MyLine(x1, y1, z1); 
      lines.Add(czg); 
     } 
     return lines; 
    } 

,而不是從外部文件(doc.LoadXml(_file)加載,XML文件必須在解決方案資源管理器和所有數據點必須能夠從那裏閱讀。

你能說我它是如何做到

問候,

馬克·吐溫

+0

謝謝。不幸的是,我是新手。你能給一個簡短的代碼片段嗎? – MarkTwain

+0

非常感謝。 – MarkTwain

回答

3

在解決

  • 將文件添加到您的解決方案。
  • 右鍵單擊文件,選擇「屬性」。
  • 改變「建設行動」 =>「嵌入的資源」

在你的代碼

  • 使用Assembly類來獲得一個裁判包含嵌入的資源(即議會大會。 GetExecutingAssembly())
  • 使用程序集ref獲取流的文件(即assemblyRef.GetManifestResourceStream(「assemblyName.filename.xml」))
  • XML文檔應該能夠加載流。

比方說我總成被稱爲「Hello.World」和文件被嵌入在項目的根目錄中稱爲「foo.xml」中,你可以使用此代碼:

var myAss = Assembly.GetExecutingAssembly(); 
using (var s = myAss.GetManifestResourceStream(string.Format("{0}.foo.xml", 
               myAss.GetName().Name))) 
{ 
    var doc = new XmlDocument(); 
    doc.Load(s); 
} 
相關問題