Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference xml file in Class library project

I have a class library project. In one of the classes I need to access an XML file. How do I reference the path to this file in a class? The file is located in one of the folders of the same project.

like image 277
user228777 Avatar asked May 20 '11 22:05

user228777


People also ask

How do I add a reference to a class library?

On the Menu bar, you would click Project -> Add Reference or In the Solution Explorer, you would right-click References and click Add Reference. You can click the Browse tab, locate the folder where the library resides and select it.

Where does XML go in Visual Studio project?

Add the XML file to Visual Studio Project Open up Visual Studio and create a new project or open an existing one. Add an existing item to the C# project. – Select the XML file we created earlier and press Add. – The file will be added to the project.

What is libraries XML file?

The Service Definition Library XML file (ServiceDef) is the mandatory input for creating a Java EE or a PL/SQL service provider. This file should contain all the details about the Web services that need to be created.

What is XML document class?

The XmlDocument class is an in-memory representation of an XML document. It implements the W3C XML Document Object Model (DOM) Level 1 Core and the Core DOM Level 2. DOM stands for document object model. To read more about it, see XML Document Object Model (DOM).


2 Answers

If you specify the Xml file to be compiled into your assembly you can read it at runtime using reflection.

Assembly asm = Assembly.GetExecutingAssembly();
XmlDocument doc = new XmlDocument();
XmlTextReader reader = new XmlTextReader(asm.GetManifestResourceStream("MyNamespace.MyXmlFile.xml"));
doc.Load(reader);

Update

Since the X509Certificate2 constructor will only accept a file path to your certificate file or a byte array you might want to use a configurable path to your certificate file instead of embedding it into your assembly.

like image 58
Filburt Avatar answered Oct 21 '22 19:10

Filburt


Using the link that @Filburt provided:

First change the BuildAction of your XML file to Embedded resource. It will be added to your assembly with the root namespace of your assembly and the filename: For example, if the root namespace of your project is MyNamespace, a resource might be named MyNamespace.MyXmlFile.xml

   Assembly _assembly;
   StreamReader _textStreamReader;
   _assembly = Assembly.GetExecutingAssembly();
   _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNameSpace.MyXmlFile.xml"));

You could use any number of classes that take a stream as a constructor parameter.

like image 3
Jeff Martin Avatar answered Oct 21 '22 19:10

Jeff Martin