Reputation: 23275
Is it possible to store an actual XML file in a Visual Studio 2010 project and then refer to it in code?
ie. Can I store an XML file (see fragment below) and then refer to data elements within the XML data? How can this be done and how would you refer to data elements deep within the XML data using C# or VB code?
If the XML file is added to the project, will it be embedded into the DLL or EXE when compiled? If not, how can it be embedded into the DLL or EXE?
<readReferenceDataResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<readReferenceDataResult xmlns="http://test.website.com/hi/xsd/providermessages/ReadReferenceData/5.6.9">
<elementReferenceValues>
<elementName>organisationTypeCode</elementName>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">1678</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Accupuncture and Eastern Medicine</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">6454</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Technical and Laboratory Services</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">9782</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Naturopathy and Natural Health</referenceDescription>
</referenceSet>
<referenceSet>
<referenceCode xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9>8557</referenceCode>
<referenceDescription xmlns="http://test.website.com/hi/xsd/providercore/Elements/5.6.9">Aged Care Services</referenceDescription>
Upvotes: 0
Views: 3239
Reputation: 3987
Just add a resource file (.resx
) to your project if one is not already present. Select File as the resource type and drag your XML file into that and give it a name (if you don't like the automatically generated one).
This will create an embedded resource type which you can then access in code by the automatically generated class representing the resource, so for example if your project is called Foo
and you add a resource file called Resources
and add an XML file resource called MyData
, then you can access that resource at Foo.Resources.MyData
in code. This will expose the contents of the file as a string, which you can then load into an XmlDocument
object when you need to by using XmlDocument.LoadXml
.
Upvotes: 3
Reputation: 365
By the sounds of it, the best solution I can think of would be using XmlDocument
You use the XmlDocument
to hold the XML file you want to read and then you can retrieve the elements/nodes from there.
If you wanted something a bit faster you can also use Linq to XML
, which is nice and simple to use.
Finally, if you have memory constraints/don't want to load the whole document into memory to read it every so often you can use XmlReader
. Which allows you to read it directly from file, node by node. It is however forward only, and is a bit tricky to use.
Upvotes: 0