简体   繁体   中英

XML file in C# solution explorer

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;
    }

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.

Could you say me how it can be done.

Regards,

Mark Twain

In the solution

  • Add the file to your solution.
  • Right click on file, select "Properties".
  • Change the "Build Action" => "Embedded Resource"

In your code

  • Use the Assembly class to get a ref to the assembly that contains the embedded resource (ie Assembly.GetExecutingAssembly())
  • Use the assembly ref to get a stream to the file (ie assemblyRef.GetManifestResourceStream("assemblyName.filename.xml"))
  • XML document should be able to load a stream.

Lets say my assembly was called "Hello.World" and the file was embedded at the root of the project called "foo.xml" the you could use this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM