In this article I will explain with an example, how to read and display XML file from Embedded Resources in Windows Application (Windows Forms Application) using C# and VB.Net.
The XML file will be displayed in structured format using a Multiline TextBox control in Windows Application (Windows Forms Application) using C# and VB.Net.
 
 
Concept
In order to display XML file in Multiline TextBox control in a Windows Application, the XML file is added to the project as an Embedded Resource. For more details please read my article, Embed and read files in Windows Application (Windows Forms) in C# and VB.Net.
Finally the contents of the XML file are loaded into the Multiline TextBox control.
 
 
XML File
Following XML file will be read from the embedded resource and displayed.
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
 <Table>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <ContactName>Maria </ContactName>
    <ContactTitle>Sales Representative</ContactTitle>
    <Address>Obere Str. 57</Address>
    <City>Boise</City>
    <PostalCode>12209</PostalCode>
    <Country>Germany</Country>
    <Phone>030-0074321</Phone>
    <Fax>030-0076545</Fax>
 </Table>
 <Table>
    <CustomerID>ANATR</CustomerID>
    <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
    <ContactName>Ana Trujillo</ContactName>
    <ContactTitle>Owner</ContactTitle>
    <Address>Avda. de la Constitución 2222</Address>
    <City>México D.F.</City>
    <PostalCode>05021</PostalCode>
    <Country>Mexico</Country>
    <Phone>(5) 555-4729</Phone>
    <Fax>(5) 555-3745</Fax>
 </Table>
 <Table>
    <CustomerID>ANTON</CustomerID>
    <CompanyName>Antonio Moreno Taquería</CompanyName>
    <ContactName>Antonio Moreno</ContactName>
    <ContactTitle>Owner</ContactTitle>
    <Address>Mataderos 2312</Address>
    <City>Montréal</City>
    <PostalCode>05023</PostalCode>
    <Country>Mexico</Country>
    <Phone>(5) 555-3932</Phone>
 </Table>
 <Table>
    <CustomerID>AROUT</CustomerID>
    <CompanyName>Around the Horn</CompanyName>
    <ContactName>Thomas Hardy</ContactName>
    <ContactTitle>Sales Representative</ContactTitle>
    <Address>120 Hanover Sq.</Address>
    <City>Mannheim</City>
    <PostalCode>WA1 1DP</PostalCode>
    <Country>Sweden</Country>
    <Phone>(171) 555-7788</Phone>
    <Fax>(171) 555-6750</Fax>
 </Table>
 <Table>
    <CustomerID>BERGS</CustomerID>
    <CompanyName>Berglunds snabbköp</CompanyName>
    <ContactName>Christina Berglund</ContactName>
    <ContactTitle>Order Administrator</ContactTitle>
    <Address>Berguvsvägen 8</Address>
    <City>Luleå</City>
    <PostalCode>S-958 22</PostalCode>
    <Country>Sweden</Country>
    <Phone>0921-12 34 65</Phone>
    <Fax>0921-12 34 67</Fax>
 </Table>
 <Table>
    <CustomerID>BLAUS</CustomerID>
    <CompanyName>Blauer See Delikatessen</CompanyName>
    <ContactName>Hanna Moos</ContactName>
    <ContactTitle>Sales Representative</ContactTitle>
    <Address>Forsterstr. 57</Address>
    <City>Mannheim</City>
    <PostalCode>68306</PostalCode>
    <Country>Germany</Country>
    <Phone>0621-08460</Phone>
    <Fax>0621-08924</Fax>
 </Table>
 <Table>
    <CustomerID>BLONP</CustomerID>
    <CompanyName>Blondesddsl père et fils</CompanyName>
    <ContactName>Frédérique Citeaux</ContactName>
    <ContactTitle>Marketing Manager</ContactTitle>
    <Address>24, place Kléber</Address>
    <City>Strasbourg</City>
    <PostalCode>67000</PostalCode>
    <Country>France</Country>
    <Phone>88.60.15.31</Phone>
    <Fax>88.60.15.32</Fax>
 </Table>
 <Table>
    <CustomerID>BOLID</CustomerID>
    <CompanyName>Bólido Comidas preparadas</CompanyName>
    <ContactName>Martín Sommer</ContactName>
    <ContactTitle>Owner</ContactTitle>
    <Address>C/ Araquil, 67</Address>
    <City>Madrid</City>
    <PostalCode>28023</PostalCode>
    <Country>Spain</Country>
    <Phone>(91) 555 22 82</Phone>
    <Fax>(91) 555 91 99</Fax>
 </Table>
 <Table>
    <CustomerID>BONAP</CustomerID>
    <CompanyName>Bon app'</CompanyName>
    <ContactName>Laurence Lebihan</ContactName>
    <ContactTitle>Owner</ContactTitle>
    <Address>12, rue des Bouchers</Address>
    <City>Marseille</City>
    <PostalCode>13008</PostalCode>
    <Country>France</Country>
    <Phone>91.24.45.40</Phone>
    <Fax>91.24.45.41</Fax>
 </Table>
 <Table>
    <CustomerID>BOTTM</CustomerID>
    <CompanyName>Bottom-Dollar Markets</CompanyName>
    <ContactName>Elizabeth Lincoln</ContactName>
    <ContactTitle>Accounting Manager</ContactTitle>
    <Address>23 Tsawassen Blvd.</Address>
    <City>Tsawassen</City>
    <Region>BC</Region>
    <PostalCode>T2F 8M4</PostalCode>
    <Country>Canada</Country>
    <Phone>(604) 555-4729</Phone>
    <Fax>(604) 555-3745</Fax>
 </Table>
</NewDataSet>
 
 
Form Controls
The following Form consists of a TextBox control. For the TextBox, the Multiline property is set to True, ReadOnly property is set to True and Scrollbars property is set to Vertical.
Read and display XML file from Embedded Resources in Windows Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Reflection;
 
VB.Net
Imports System.IO
Imports System.Reflection
 
 
Read and display XML file from Embedded Resources in Windows Application
Inside the Form Load event handler, first an object of the Assembly class is created and it is assigned the reference of the executing assembly.
Then the contents of the XML file are read using a StreamReader class object using the GetManifestResourceStream function of the Assembly class.
Finally the contents of the StreamReader are read using the ReadToEnd method and are assigned to the Multiline TextBox control.
C#
private void Form1_Load(object sender, EventArgs e)
{
    Assembly assembly = Assembly.GetExecutingAssembly();
    StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("Read_Embedded_XML_File_CS.Customers.xml"));
    txtXML.Text = reader.ReadToEnd();
}
 
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim assmbly As Assembly = Assembly.GetExecutingAssembly()
    Dim reader As New StreamReader(assmbly.GetManifestResourceStream("Read_Embedded_XML_File_VB.Customers.xml"))
    txtXML.Text = reader.ReadToEnd()
End Sub
 
 
Screenshot
Read and display XML file from Embedded Resources in Windows Application using C# and VB.Net
 
 
Downloads