C#中的XML文件和数据读取 [英] XML file and data reading in C#
问题描述
我有一个3D对象及其数据点.数据由1000点组成.
以下是数据点.
I have a 3D object and their data points. Data consists of 1000 points.
The following are for the data points.
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文件必须位于解决方案资源管理器中,并且所有数据点都必须能够从那里读取.
该行"doc.LoadXml(_file);"必须删除.也就是说,从XML文件读取的数据必须不需要"OpenFileDialog",并且必须在代码内部.
你能告诉我怎么做吗?
问候,
Mark
Instead of loading from an external file (doc.LoadXml(_file), the XML file must be in solution explorer and all data points must be able to read from there.
The line "doc.LoadXml(_file);" must e removed. That is, data reading from XML file must not need "OpenFileDialog" and must be inside the code.
Could you say me how it can be done.
Regards,
Mark
推荐答案
我建议使用LINQ来解析XML,这很整洁.
I recommend using LINQ to parse your XML, it''s much neater.
StreamResourceInfo sri = System.Windows.Application.GetResourceStream(new System.Uri("pack://application:,,,/MediaData/germanDiaIndex.xml"));
XmlTextReader xmlReader = new XmlTextReader(sri.Stream);
是我的应用从资源读取XML文件的方式.如您所见,sri.Stream为您提供了用于读取数据的流,如果将其移动到LINQ,我敢肯定您可以使用流来做到这一点.
is how my app reads an XML file from resources. As you can see, sri.Stream gives you a stream to use to read the data, if you move it to LINQ, I''m sure you can use a stream to do that.
一种解决方案是将XML文件包含在项目资源中.有关如何对Jet DB执行此操作的示例,请参见此链接 [ ^ ],其中您可以修改以包含所需的任何类型的文件.
One solution is to include the XML file in your project resources. For an example of how to do this for a Jet DB, see this link[^], which you can modify to include any sort of file that you want.
这篇关于C#中的XML文件和数据读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!