开发者

xml technique with visual studio projects

开发者 https://www.devze.com 2023-01-07 17:13 出处:网络
I am programming in .NET (C#) using Visual Studio. I want to use am XML file to store some information in my program. No other programs will ever need to access this XML file and no one will need to a

I am programming in .NET (C#) using Visual Studio. I want to use am XML file to store some information in my program. No other programs will ever need to access this XML file and no one will need to access/modify it manually our without using the program. I am wondering what build options I should select for this file or if it really makes a difference? Should it be an embedded resource or content file, etc? If i coul开发者_开发技巧d build it right in to the .exe I would but I don't think that is possible. Also, if I have to do anything special to reference the file please comment on that.


Add your file to the project as a resource. See this MSDN article for how to do this.

By using this method, the resource is added to your assembly and is exposed as a strongly-typed property of the Resources class (BaseNamespace.Properties.Resources). It is much easier to use than the old school GetManifestResourceStream version, plus you get compile time safety.


Set the Build Action to be Embedded Resource, then use code something like this to load it into an XML Document object...

Dim objResource As New Xml.XmlDocument

objResource.Load(Assembly.GetExecutingAssembly.GetManifestResourceStream("Resource.xml"))

UPDATE See Will's correct comments below, in which case the code (in VB.Net) would be this instead...

Dim objResource As New Xml.XmlDocument

objResource.LoadXml(My.Resources.ResourceName)


You can add your xml file as an embedded resource and then load it through Assembly.GetManifestResourceStream method

var pathToAssembly = "c:\\assembly.dll";
var assembly = Assembly.LoadForm(pathToAssembly);
var pathToResourceWithinAssembly = "assembly.YourEmbeddedXML.xml"; //assembly is the name of your assembly where embedded resource defined
var stream = assembly.GetManifestResourceStream(pathToResourceWithinAssembly);
var ebeddedXML = string.Empty;
if (stream != null)
            {
                var streamReader = new StreamReader(stream);
                ebeddedXML = streamReader.ReadToEnd();
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号