Greetings Good People of DaniWeb,

I have been on a search for a solution for my problem for weeks and weeks now. My search has brought me into this wonderful community in which I hope to find help. I will be doing my part in the community by providing help as well when it comes to matters of my expertise.

Right now I need your help. :)

I am working on a Thesis Software. Right now I am stuck and don't know what to do.

My software needs to write xml files (Figured it out) and also READ XML Files then use the values inside the XML files as data by assigning the values to variables and such for the software. In essence we are using XML Files as a type of flat file database.

One of my XML files has the following structure:

<User>
  <Student ID="1">
    <FirstName> </FirstName>
    <LastName> </LastName>
    <StudentNumber> </StudentNumber>
  </Student>
</User>

I need to be able to read the xml file and get the values inside FirstName and put it into a variable that I can use through the system(like show the firstname in a label) and the same with LastName and StudentNumber. I would like to note that there will only be one student. This particular XML file is for a Student Profile.

Also I would like to note that the location of the will depend on the programs start up location:

System.AppDomain.CurrentDomain.BaseDirectory() & "StudentProfile.xml"

Please help. Let me know your thoughts.

I will be truly grateful.

Thank you so much and have a blessed day!

There are number of ways(methods) to read/write XML document.

C#

Method:1 System.XML - DOM API

string file=@"sample.xml";
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(file);

            //Iterate "Student"
            foreach (System.Xml.XmlElement student in doc.DocumentElement.ChildNodes)
            {

                foreach (System.Xml.XmlElement field in student.ChildNodes)
                {
                    MessageBox.Show(field.Name + " : " + field.InnerText);
                }

            }

Method:2 System.XML.Linq

string file=@"sample.xml";
            XDocument doc = XDocument.Load(file);

            foreach (XElement student in doc.Descendants("Student"))
            {
                if (student.HasAttributes)
                {
                    MessageBox.Show("ID : " + student.Attribute("ID").Value);
                }

                if (student.HasElements)
                {
                    MessageBox.Show(student.Element("FirstName").Value);
                }

            }

Method:3 DataSet/DataTable

string file=@"sample.xml";
            DataSet ds = new DataSet();
            ds.ReadXml(file);

            foreach (DataRow row in ds.Tables["Student"].Rows )
            {
                MessageBox.Show(row["ID"].ToString() + " " + row["FirstName"].ToString());
            }

VB.NET

Method:1 System.XML - DOM API

Dim file As String = "sample.xml"
Dim doc As New System.Xml.XmlDocument()
doc.Load(file)

'Iterate "Student"
For Each student As System.Xml.XmlElement In doc.DocumentElement.ChildNodes

	For Each field As System.Xml.XmlElement In student.ChildNodes
		MessageBox.Show(field.Name & " : " & field.InnerText)

	Next
Next

Method:2 System.XML.Linq

Dim file As String = "sample.xml"
Dim doc As XDocument = XDocument.Load(file)

For Each student As XElement In doc.Descendants("Student")
	If student.HasAttributes Then
		MessageBox.Show("ID : " + student.Attribute("ID").Value)
	End If

	If student.HasElements Then
		MessageBox.Show(student.Element("FirstName").Value)

	End If
Next

Method:3 DataSet/DataTable

Dim file As String = "sample.xml"
Dim ds As New DataSet()
ds.ReadXml(file)

For Each row As DataRow In ds.Tables("Student").Rows
	MessageBox.Show(row("ID").ToString() & " " & row("FirstName").ToString())
Next

I am extremely grateful mister adatapost. Thank you! The code you have provided have worked flawlessly.

Again, Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.