2011-08-15 126 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); 
} 
相关问题